Java Two Diff Solutions O(m^2+n^2) and O(m*n)
Method1: \n1. Use two different hashmaps\n2. Add entried to map for diff combinations of product\n3. Iterate over arrays and check if square of num exists in ot
Method1: \n1. Use two different hashmaps\n2. Add entried to map for diff combinations of product\n3. Iterate over arrays and check if square of num exists in other map\n\nTime: O(m^2 + n^2)\n```\nclass Solution {\n    public int numTriplets(int[] nums1, int[] nums2) {\n        Map<Long, Integer> productSet1 = new HashMap<>();\n        Map<Long, Integer> productSet2 = new HashMap<>();\n        int m = nums1.length, n = nums2.length;\n        for(int i=0; i<m; i++) {\n            for(int j=i+1; j<m; j++) {\n                long product = (long) nums1[i]*nums1[j];\n                productSet1.put(product, productSet1.getOrDefault(product, 0)+1);\n            }\n        }\n        for(int i=0; i<n; i++) {\n            for(int j=i+1; j<n; j++) {\n                long product = (long) nums2[i]*nums2[j];\n                productSet2.put(product, productSet2.getOrDefault(product, 0)+1);\n            }\n        }\n        int count =0;\n        for(int i=0; i<m; i++) {\n            count+= productSet2.getOrDefault((long) nums1[i]*nums1[i], 0);\n        }\n        for(int i=0; i<n; i++) {\n            count+= productSet1.getOrDefault((long) nums2[i]*nums2[i], 0);\n        }\n        return count;\n    }\n}\n```\n\nMethod2:  \n1. Sort both arrays\n2. Use a hashmap for two arrays to keep count of elements\n2. Use two pointer approach to look for any valid j and k indices in other array\n3. Take care of duplicates, via hashmap and update pointers\n\nTime: O(m*n)\n```\n    public int findCount(int[] nums1, int[] nums2, Map<Integer, Integer> map) {\n        int n=nums2.length, ans=0;\n        for(int num: nums1) {\n            int lo=0, hi=n-1;\n            long target = (long) num*num;\n            while(lo<hi) {\n                long product = (long) nums2[lo]*nums2[hi];\n                if(product<target)  lo++;\n                else if(product>target) hi--;\n                else {\n                    int count1 = map.get(nums2[lo]);\n                    int count2 = map.get(nums2[hi]);\n                    if(nums2[lo]!=nums2[hi]) {\n                        ans+= count1*count2;\n                    } else {\n                        ans+= count1*(count1-1)/2;\n                    }\n                    while(lo<hi && nums2[lo]==nums2[lo+1]) lo++;\n                    while(hi>lo && nums2[hi]==nums2[hi-1]) hi--;\n                    lo++; hi--;\n                }\n            }\n        }\n        return ans;\n    }\n    \n    public int numTriplets(int[] nums1, int[] nums2) {\n        Arrays.sort(nums1);\n        Arrays.sort(nums2);\n        \n        Map<Integer, Integer> map1 = new HashMap<>();\n        Map<Integer, Integer> map2 = new HashMap<>();\n        \n        for(int num: nums1)\n            map1.put(num, map1.getOrDefault(num, 0)+1);\n        for(int num: nums2)\n            map2.put(num, map2.getOrDefault(num, 0)+1);\n        \n        return findCount(nums1, nums2, map2) + findCount(nums2, nums1, map1);\n    }\n}\n```
['Sorting', 'Java']