C++ | Two Pointer | 100 % faster
\nclass Solution {\npublic:\n    int countTriplet(vector<int>& nums1, vector<int>& nums2) {\n        int count = 0, left, right;\n        long num1Val, num2Val;
```\nclass Solution {\npublic:\n    int countTriplet(vector<int>& nums1, vector<int>& nums2) {\n        int count = 0, left, right;\n        long num1Val, num2Val;\n        unordered_map<int, int> freq;\n        \n        for(int i = 0; i < nums2.size(); i++) freq[nums2[i]]++;\n        \n        for(int i = 0; i < nums1.size(); i++) {\n            left = 0;\n            right = nums2.size()-1;\n            num1Val = (long)nums1[i] * nums1[i];\n            while(left < right) {\n                num2Val = (long)nums2[left] * nums2[right];\n                if(num1Val == num2Val) {\n                    if(nums2[right] == nums2[left]) {\n                        count += (freq[nums2[right]] * (freq[nums2[right]]-1))/2;\n                        break;\n                    }\n                    count += freq[nums2[right]] * freq[nums2[left]];\n                    left  += freq[nums2[left]];\n                    right -= freq[nums2[right]];\n                } else if(num1Val > num2Val) {\n                    left++;\n                } else {\n                    right--;\n                }\n            }\n        }\n        return count;\n    }\n    \n    int numTriplets(vector<int>& nums1, vector<int>& nums2) {\n        sort(nums1.begin(), nums1.end());\n        sort(nums2.begin(), nums2.end());\n        return countTriplet(nums1, nums2) + countTriplet(nums2, nums1);\n    }\n};\n```
[]