Given two <strong>sorted 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> as well as an integer <code>k</code>, return <em>the </em><code>k<sup>th</sup></code><em> (<strong>1-based</strong>) smallest product of </em><code>nums1[i] * nums2[j]</code><em> where </em><code>0 &lt;= i &lt; nums1.length</code><em> and </em><code>0 &lt;= j &lt; nums2.length</code>.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [2,5], nums2 = [3,4], k = 2
<strong>Output:</strong> 8
<strong>Explanation:</strong> The 2 smallest products are:
- nums1[0] * nums2[0] = 2 * 3 = 6
- nums1[0] * nums2[1] = 2 * 4 = 8
The 2<sup>nd</sup> smallest product is 8.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6
<strong>Output:</strong> 0
<strong>Explanation:</strong> The 6 smallest products are:
- nums1[0] * nums2[1] = (-4) * 4 = -16
- nums1[0] * nums2[0] = (-4) * 2 = -8
- nums1[1] * nums2[1] = (-2) * 4 = -8
- nums1[1] * nums2[0] = (-2) * 2 = -4
- nums1[2] * nums2[0] = 0 * 2 = 0
- nums1[2] * nums2[1] = 0 * 4 = 0
The 6<sup>th</sup> smallest product is 0.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3
<strong>Output:</strong> -6
<strong>Explanation:</strong> The 3 smallest products are:
- nums1[0] * nums2[4] = (-2) * 5 = -10
- nums1[0] * nums2[3] = (-2) * 4 = -8
- nums1[4] * nums2[0] = 2 * (-3) = -6
The 3<sup>rd</sup> smallest product is -6.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>-10<sup>5</sup> &lt;= nums1[i], nums2[j] &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums1.length * nums2.length</code></li>
	<li><code>nums1</code> and <code>nums2</code> are sorted.</li>
</ul>

class Solution:
    def kthSmallestProduct(self, nums1: List[int], nums2: List[int], k: int) -> int:
        posIdx1 = bisect_left(nums1,0)
        posIdx2 = bisect_left(nums2,0)
        # posIdxi = first index of numsi such that numsi[j] >= 0 for all j >= posIdxi
        # (-)(-) = +, backwards
        # (-)(+), (+)(-) = -, backwards
        # (+)(+) = +, forwards

        def count(val):
            ans = 0
            if val >= 0:
                # all negative guys
                ans += (posIdx1)*(len(nums2)-posIdx2)
                ans += (posIdx2)*(len(nums1)-posIdx1)
                # product of two positives
                j = len(nums2)-1
                for i in range(posIdx1,len(nums1)):
                    while j >= posIdx2 and nums1[i]*nums2[j] > val:
                        j -= 1
                    ans += j - posIdx2 + 1
                # product of two negatives
                j=0
                for i in range(posIdx1-1,-1,-1):
                    while j < posIdx2 and nums1[i]*nums2[j] > val:
                        j += 1
                    ans += posIdx2-j
            else:
                j=posIdx2 - 1
                for i in range(len(nums1)-1,posIdx1-1,-1):
                    while j >= 0 and nums1[i]*nums2[j] > val:
                        j -= 1
                    ans += j+1
                i = posIdx1 - 1
                for j in range(len(nums2)-1,posIdx2-1,-1):
                    while i >= 0 and nums1[i]*nums2[j] > val:
                        i -= 1
                    ans += i+1
            return ans

        l = min(nums1[0]*nums2[0],
                nums1[0]*nums2[-1],
                nums1[-1]*nums2[0])
        r = max(nums1[0]*nums2[0],
                nums1[-1]*nums2[-1],
                nums1[0]*nums2[-1],
                nums2[0]*nums1[-1])
        while l <= r:
            m = l + (r-l)//2
            if count(m) < k:
                l = m+1
            else:
                r = m-1
        return l
26700