Java Solution with HashMap and binary search
Intuition\nCounting, HashMap and binary search\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity:\n Add your ti
# Intuition\nCounting, HashMap and binary search\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n\n    public int numTriplets(int[] nums1, int[] nums2) {\n        Map<Integer, List<Integer>> valIndexHolder = getMap(nums2);\n        int ans = 0;\n\n        for (int num : nums1) {\n            long n = (long)num * num;\n            ans += getCount(valIndexHolder, n);\n        }\n        valIndexHolder = getMap(nums1);\n\n        for (int num : nums2) {\n            long n = (long)num * num;\n            ans += getCount(valIndexHolder, n);\n        }\n        return ans;\n    }\n\n    private int getCount(Map<Integer, List<Integer>> valIndexHolder, long sqNum) {\n        int ans = 0;\n\n        for (int val : valIndexHolder.keySet()) {\n\n            if (sqNum % val != 0) {\n                continue;\n            }\n            int result = (int)(sqNum / val);\n            List<Integer> resultList = valIndexHolder.getOrDefault(result, new ArrayList<>());\n\n            if (result == val) {\n\n                if (resultList.size() > 1) {\n                    ans += resultList.size() * (resultList.size() - 1) / 2;\n                }\n            } else {\n                List<Integer> valList = valIndexHolder.get(val);\n                int i = 0;\n\n                for (; i < valList.size(); i++) {\n                    int index = binarySearchGreaterThan(resultList, valList.get(i));\n\n                    if (index != -1) {\n                        ans += resultList.size() - index;\n                    }\n                }\n            }\n        }\n        return ans;\n    }\n\n    private int binarySearchGreaterThan(List<Integer> resultList, int index) {\n        int lo = 0;\n        int hi = resultList.size() - 1;\n        int ans = -1;\n\n        while (lo <= hi) {\n            int pivot = lo + (hi - lo) / 2;\n\n            if (resultList.get(pivot) < index) {\n                lo = pivot + 1;\n            } else {\n                hi = pivot - 1;\n                ans = pivot;\n            }\n        }\n        return ans;\n    }\n\n    private Map<Integer, List<Integer>> getMap(int[] nums) {\n        Map<Integer, List<Integer>> valIndexHolder = new HashMap<>();\n\n        for (int i = 0; i < nums.length; i++) {\n            List<Integer> list = valIndexHolder.getOrDefault(nums[i], new ArrayList<>());\n            list.add(i);\n            valIndexHolder.put(nums[i], list);\n        }\n        return valIndexHolder;\n    }\n}\n```
['Hash Table', 'Binary Search', 'Counting', 'Java']