C++ | Both Approaches | 2 Pointer & Binary search | Easy to understand |
Approach 1 : 2 pointers\n\nclass Solution {\npublic:\n    long long kthSmallestProduct(vector<int>& nums1, vector<int>& nums2, long long k) {\n        \n       
**Approach 1 : 2 pointers**\n```\nclass Solution {\npublic:\n    long long kthSmallestProduct(vector<int>& nums1, vector<int>& nums2, long long k) {\n        \n        auto fn = [&](double val) {\n            long long ans = 0; \n            for (auto& x : nums1) {\n                if (x < 0) ans += nums2.end() - lower_bound(nums2.begin(), nums2.end(), ceil(val/x)); \n                else if (x == 0) {\n                    if (0 <= val) ans += nums2.size(); \n                } else ans += upper_bound(nums2.begin(), nums2.end(), floor(val/x)) - nums2.begin(); \n            }\n            return ans; \n        }; \n        \n        long long lo = -pow(10ll, 10), hi = pow(10ll, 10)+1;\n        while (lo < hi) {\n            long long mid = lo + (hi - lo)/2; \n            if (fn(mid) < k) lo = mid + 1; \n            else hi = mid; \n        }\n        return lo; \n    }\n};\n\n```\n**Approach 2 : Binary Search**\nThe idea of this problem is to use binary search on answer -\nLet check(x) be the answer to the question: how many products are less or equal than x.\nThen we use binary search and find the first moment where we have exactly k such numbers .\n```\nclass Solution {\npublic:\n    bool check(long long midval,vector<int>& nums1, vector<int>& nums2, long long k){\n        long long cnt=0;\n        for(int i=0;i<nums1.size();i++)\n        {\n            long long val=nums1[i];\n                        \n\t\t\t//If val == 0, product of val and each element in nums2 will be 0. And if midval>=0, then because all\n\t\t\t//products are 0, all products will be smaller or equal to midval. So we can add all products in the answer\n\t\t\tif(val==0 and midval>=0)\n                cnt+=nums2.size();\n            \n            else if(val>0)\n                cnt+=findmaxIndex(nums2,val,midval);\n            \n            else if(val<0)\n                cnt+=findminIndex(nums2,val,midval);\n        }\n        return cnt>=k;\n    }\n    \n    int findmaxIndex(vector<int>&nums2 , long long  val , long long midval)\n    {\n        int l = 0  , r = nums2.size()-1 , res=  -1;\n        while(l<=r)\n        {\n            long long mid = (l+r)/2;\n            if(val*nums2[mid]<=midval)\n            {\n                res=mid;\n                l=mid+1;\n            }\n            else r=mid-1;\n        }\n        return res+1;\n    }\n    \n    int findminIndex(vector<int>&nums2 , long long  val , long long midval)\n    {\n        int l = 0  , r = nums2.size()-1 , res=  r+1;\n        while(l<=r)\n        {\n            long long mid = (l+r)/2;\n            if(val*nums2[mid]<=midval)\n            {\n                res=mid;\n                r=mid-1;\n            }\n            else l=mid+1;\n        }\n        return nums2.size()-res;\n    }\n    \n    long long kthSmallestProduct(vector<int>& nums1, vector<int>& nums2, long long k) {\n        long long l=-1e10,r=1e10,res=-1;\n        while(l<=r){\n            long long mid = (l+r)/2;\n            // cout<<mid<<endl;\n            if(check(mid,nums1,nums2,k)) {\n                res=mid;\n                r=mid-1;\n            }\n            else l=mid+1;\n        }\n        return res;\n    }\n};\n```
['Two Pointers', 'Binary Search', 'C', 'Binary Tree', 'C++']