Given two sorted 0-indexed integer arrays nums1 and nums2 as well as an integer k, return the k^th (1-based ) smallest product of nums1[i] * nums2[j] where 0 <= i < nums1.length and 0 <= j < nums2.length.
// count how many products are less than x // use a and b to refer to nums1[i] and nums2[j] below // a * b <= x b <= x / a boolisPossible(longlong x, vector<int>& nums1, vector<int>& nums2, longlong k){ int n1 = nums1.size(), n2 = nums2.size(); longlong count = 0; for (int i = 0; i < n1; i++) { if (nums1[i] < 0) { longlong rem = ceil((double)x / nums1[i]); int idx = lower_bound(nums2.begin(), nums2.end(), rem) - nums2.begin(); count += (n2 - idx); } elseif (nums1[i] > 0) { longlong rem = floor((double)x / nums1[i]); int idx = upper_bound(nums2.begin(), nums2.end(), rem) -nums2.begin(); count += idx; } else { if (x >= 0) { count += n2; } } if (count >= k) { returntrue; } } return count >= k; } };