Straightforward solution - brute force with optimized search using set
\nclass Solution {\npublic:\n    bool perfectSquare(long long int n) {\n        long long int ans = sqrt(n);\n        return n == ans*ans;\n    }\n    \n    int
```\nclass Solution {\npublic:\n    bool perfectSquare(long long int n) {\n        long long int ans = sqrt(n);\n        return n == ans*ans;\n    }\n    \n    int squareRoot(long long int n) {\n        int ans = sqrt(n);\n        return ans;\n    }\n    \n    int findTriplets(vector<int> a, vector<int> b) {\n        int ans = 0;\n        unordered_map<int, int> frequency;\n        for (auto &y: b) {\n            frequency[y]++;\n        }\n        int n = a.size();\n        for (int i=0; i<n-1; i++) { \n            for (int j=i+1; j<n; j++) {\n                long long int num = a[i];\n                num *= a[j];\n                if (perfectSquare(num)) {\n                    ans += frequency[squareRoot(num)];\n                }\n            }\n        }\n        return ans;\n    }\n    \n    int numTriplets(vector<int>& nums1, vector<int>& nums2) {\n        return findTriplets(nums1, nums2) + findTriplets(nums2, nums1);\n    }\n};\n```
[]