2 pointer approach || O(1) space complexity || 98% faster
\nclass Solution {\n    public int numTriplets(int[] nums1, int[] nums2) {\n        Arrays.sort(nums1);\n        Arrays.sort(nums2);\n        return count(nums1
```\nclass Solution {\n    public int numTriplets(int[] nums1, int[] nums2) {\n        Arrays.sort(nums1);\n        Arrays.sort(nums2);\n        return count(nums1 , nums2) + count(nums2 , nums1);\n    }\n    \n    public int count(int a[] , int b[]){\n        int n = a.length;\n        int m = b.length;\n        int count = 0;\n        for(int i=0;i<n;i++){\n            long x = (long)a[i]*a[i];\n            int j = 0;\n            int k = m-1;\n            while(j<k){\n                long prod = (long)b[j]*b[k];\n                if(prod<x)\n                    j++;\n                else if(prod>x)\n                    k--;\n                else if(b[j] != b[k]){\n                    int jNew = j;\n                    int kNew = k;\n                    \n                    while(b[j] == b[jNew])\n                        jNew++;\n                    while(b[k] == b[kNew])\n                        kNew--;\n                    count += (jNew-j)*(k-kNew);\n                    j = jNew;\n                    k = kNew;\n                }\n                else{\n                    int q = k-j+1;\n                    count += (q)*(q-1)/2;\n                    break;\n                }\n            }\n        }\n        return count;\n    }\n    \n}\n```
['Two Pointers', 'Java']