C++ Easy 3 Sum, 2 pointer (with comments) | O(N*M) time O(1) space
\n\nclass Solution {\npublic:\n    #define ll long long\n    \n\t// returns count of valids triplets (i, j, K) of type 1\n\t// note that to find of type 2, just
```\n\nclass Solution {\npublic:\n    #define ll long long\n    \n\t// returns count of valids triplets (i, j, K) of type 1\n\t// note that to find of type 2, just swap nums1 and nums2 while calling \n    int returnCount(vector<int>& nums1, vector<int>& nums2) {\n        int n = nums1.size(), m = nums2.size();\n        int count = 0;\n        \n        for(int k=0; k<n; k++) {\n            ll target = nums1[k]*1ll*nums1[k];\n\t\t\t// we\'ll find two numbers in nums2 such that their product equals target \n            int i=0, j=m-1;\n            while(i<j) {\n                ll prod = nums2[i]*1ll*nums2[j];\n                if(prod == target) {\n\t\t\t\t\t// case 1 : nums[i] == nums[j]\n\t\t\t\t\t// this means all numbers in between i & j are same and\n\t\t\t\t\t// we can choose any two out of all of them, hence Nc2 possibilities\n                    if(nums2[i] == nums2[j]) {\n                        int len = j-i+1;\n                        count += len*(len-1)/2;\n                        i = j+1;\n                    }\n\t\t\t\t\t// case 2 : if not so\n\t\t\t\t\t// find the number of elements equal to nums2[i] (say n1) and\n\t\t\t\t\t// that equal to nums2[j] ( ssay n2)\n\t\t\t\t\t// add n1*n2 to ans\n                    else {\n                        int left_len = 1, right_len = 1;\n                        while(i<j && nums2[i] == nums2[i+1]) {\n                            i++;\n                            left_len++;\n                        }\n                        while(j>i && nums2[j-1] == nums2[j]) {\n                            j--;\n                            right_len++;\n                        }\n                        \n                        count += left_len * right_len;\n                        i++; j--;\n                    }\n                    \n                }\n\t\t\t\t// accordingly move pointer i or j\n                else if(prod < target)\n                    i++;\n                else\n                    j--;\n            }\n        }\n        \n        return count;\n    }\n    \n    int numTriplets(vector<int>& nums1, vector<int>& nums2) {\n        \n        sort(nums1.begin(), nums1.end());\n        sort(nums2.begin(), nums2.end());\n        \n        return returnCount(nums1, nums2) + returnCount(nums2, nums1);\n        \n    }\n};\n\n\n```
['Two Pointers', 'C']