Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:


	Type 1: Triplet (i, j, k) if nums1[i]2 == nums2[j] * nums2[k] where 0 &lt;= i &lt; nums1.length and 0 &lt;= j &lt; k &lt; nums2.length.
	Type 2: Triplet (i, j, k) if nums2[i]2 == nums1[j] * nums1[k] where 0 &lt;= i &lt; nums2.length and 0 &lt;= j &lt; k &lt; nums1.length.


&nbsp;
Example 1:

Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1, 1, 2), nums1[1]2 = nums2[1] * nums2[2]. (42 = 2 * 8). 


Example 2:

Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 12 = 1 * 1.
Type 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2).  nums1[i]2 = nums2[j] * nums2[k].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]2 = nums1[j] * nums1[k].


Example 3:

Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2).  nums1[3]2 = nums2[0] * nums2[2].
Type 2: (3,0,1).  nums2[3]2 = nums1[0] * nums1[1].


&nbsp;
Constraints:


	1 &lt;= nums1.length, nums2.length &lt;= 1000
	1 &lt;= nums1[i], nums2[i] &lt;= 105


class Solution:
    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
        sqr1, sqr2 = defaultdict(int), defaultdict(int)
        m, n = len(nums1), len(nums2)
        for i in range(m):
            sqr1[nums1[i]**2] += 1
        for j in range(n):
            sqr2[nums2[j]**2] += 1

        res = 0
        for i in range(m-1):
            for j in range(i+1, m):
                if nums1[i]*nums1[j] in sqr2:
                    res += sqr2[nums1[i]*nums1[j]]

        for i in range(n-1):
            for j in range(i+1, n):
                if nums2[i]*nums2[j] in sqr1:
                    res += sqr1[nums2[i]*nums2[j]]
        return res
class Solution {
    public int numTriplets(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        return count(nums1 , nums2) + count(nums2 , nums1);
    }

    public int count(int a[] , int b[]){
        int n = a.length;
        int m = b.length;
        int count = 0;
        for(int i=0;i<n;i++){
            long x = (long)a[i]*a[i];
            int j = 0;
            int k = m-1;
            while(j<k){
                long prod = (long)b[j]*b[k];
                if(prod<x)
                    j++;
                else if(prod>x)
                    k--;
                else if(b[j] != b[k]){
                    int jNew = j;
                    int kNew = k;

                    while(b[j] == b[jNew])
                        jNew++;
                    while(b[k] == b[kNew])
                        kNew--;
                    count += (jNew-j)*(k-kNew);
                    j = jNew;
                    k = kNew;
                }
                else{
                    int q = k-j+1;
                    count += (q)*(q-1)/2;
                    break;
                }
            }
        }
        return count;
    }

}
class Solution {
public:
    int numTriplets(vector<int>& nums1, vector<int>& nums2) {

        unordered_map<long, int> m1, m2;
        for(int i : nums1) m1[(long long)i*i]++;
        for(int i : nums2) m2[(long long)i*i]++;

        int ans = 0;

        for(int i=0; i<nums2.size()-1; i++){
            for(int j=i+1; j<nums2.size(); j++){
                if(m1[(long long)nums2[i] * nums2[j]]){
                    ans += m1[(long long)nums2[i] * nums2[j]];
                }
            }
        }
        for(int i=0; i<nums1.size()-1; i++){
            for(int j=i+1; j<nums1.size(); j++){
                if(m2[(long long)nums1[i] * nums1[j]]){
                    ans += m2[(long long)nums1[i] * nums1[j]];
                }
            }
        }
        return ans;
    }
};
var numTriplets = function(nums1, nums2) {
    const nm1 = new Map(), nm2 = new Map();
    const n = nums1.length, m = nums2.length;
    for(let i = 0; i < n; i++) {
        for(let j = i + 1; j < n; j++) {
            const product = nums1[i] * nums1[j];
            if(!nm1.has(product)) nm1.set(product, 0);
            const arr = nm1.get(product);
            nm1.set(product, arr + 1);
        }
    }
    for(let i = 0; i < m; i++) {
        for(let j = i + 1; j < m; j++) {
            const product = nums2[i] * nums2[j];
            if(!nm2.has(product)) nm2.set(product, 0);
            const arr = nm2.get(product);
            nm2.set(product, arr + 1);
        }
    }
    let ans = 0;

    for(let num of nums1) {
        const sq = num * num;
        if(nm2.has(sq)) {
            ans += nm2.get(sq);
        }
    }
    for(let num of nums2) {
        const sq = num * num;
        if(nm1.has(sq)) {
            ans += nm1.get(sq);
        }
    }
    return ans;
};
Number of Ways Where Square of Number Is Equal to Product of Two Numbers