Given two sorted 0-indexed integer arrays nums1 and nums2 as well as an integer k, return the kth (1-based) smallest product of nums1[i] * nums2[j] where 0 <= i < nums1.length and 0 <= j < nums2.length.
There are up to \(2.5 \times 10^9\) products, so they cannot be listed. The \(k\)-th is monotone in a threshold \(p\): the count of products \(\le p\) never decreases.
Binary-search \(p\) on \([-M,M]\). For each \(x\) in \(nums1\), positives use \(nums2[i] \le p/x\), negatives reverse the inequality, and zeros contribute the whole array when \(p \ge 0\). Sortedness lets each count be a binary search.
bisect_left returns the smallest \(p\) whose count is at least \(k\).
We can use binary search to enumerate the value of the product \(p\), defining the binary search interval as \([l, r]\), where \(l = -\textit{max}(|\textit{nums1}[0]|, |\textit{nums1}[n - 1]|) \times \textit{max}(|\textit{nums2}[0]|, |\textit{nums2}[n - 1]|)\), \(r = -l\).
For each \(p\), we calculate the number of products less than or equal to \(p\). If this number is greater than or equal to \(k\), it means the \(k\)-th smallest product must be less than or equal to \(p\), so we can reduce the right endpoint of the interval to \(p\). Otherwise, we increase the left endpoint of the interval to \(p + 1\).
The key to the problem is how to calculate the number of products less than or equal to \(p\). We can enumerate each number \(x\) in \(\textit{nums1}\) and discuss in cases:
If \(x > 0\), then \(x \times \textit{nums2}[i]\) is monotonically increasing as \(i\) increases. We can use binary search to find the smallest \(i\) such that \(x \times \textit{nums2}[i] > p\). Then, \(i\) is the number of products less than or equal to \(p\), which is accumulated into the count \(\textit{cnt}\);
If \(x < 0\), then \(x \times \textit{nums2}[i]\) is monotonically decreasing as \(i\) increases. We can use binary search to find the smallest \(i\) such that \(x \times \textit{nums2}[i] \leq p\). Then, \(n - i\) is the number of products less than or equal to \(p\), which is accumulated into the count \(\textit{cnt}\);
If \(x = 0\), then \(x \times \textit{nums2}[i] = 0\). If \(p \geq 0\), then \(n\) is the number of products less than or equal to \(p\), which is accumulated into the count \(\textit{cnt}\).
This way, we can find the \(k\)-th smallest product through binary search.
The time complexity is \(O(m \times \log n \times \log M)\), where \(m\) and \(n\) are the lengths of \(\textit{nums1}\) and \(\textit{nums2}\), respectively, and \(M\) is the maximum absolute value in \(\textit{nums1}\) and \(\textit{nums2}\).