# LeetCode 2040. Kth Smallest Product of Two Sorted Arrays

``````class Solution {
public:
int p1;
int p2;
long long int a[50005];
long long int b[50005];

long long kthSmallestProduct(vector<int>& nums1, vector<int>& nums2, long long k) {

for (int i = 0; i < nums1.size(); i++)
{
a[i] = nums1[i];
}

for (int i = 0; i < nums2.size(); i++)
{
b[i] = nums2[i];
}

for (int i = 0; i < nums1.size(); i++)
{
if (a[i] >= 0)
{
p1 = i;
break;
}
}

if(a[nums1.size()-1]<0)
{
p1 = nums1.size();
}

long long int m1 = a[0];
long long int n1 = a[nums1.size()-1];

long long int m2 = b[0];
long long int n2 = b[nums2.size()-1];

long long int l = min(m1*m2, min(m1*n2, min(n1*m2, n1*n2)));
long long int r = max(m1*m2, max(m1*n2, max(n1*m2, n1*n2)));

long long int mid;

while (l <= r)
{
mid = (l + r) / 2;
long long int  x = find(mid, nums1, nums2);
if (x >= k)
{
r = mid - 1;
continue;
}

if (x < k)
{
l = mid + 1;
continue;
}
}

return l;
}

long long int find(long long int x, vector<int>& nums1, vector<int>& nums2)
{
int l, r, mid;
long long int sum = 0;
for (int i = 0; i < p1; i++)
{
l = 0;
r = nums2.size() - 1;
while (l <= r)
{
mid = (l + r) / 2;
if (a[i] * b[mid] <= x)
{
r = mid - 1;
continue;
}

if (a[i] * b[mid] > x)
{
l = mid + 1;
continue;
}
}

sum += nums2.size() - l;
}

for (int i = p1; i < nums1.size(); i++)
{
l = 0;
r = nums2.size()-1;
while (l <= r)
{
mid = (l + r) / 2;
if (a[i] * b[mid] <= x)
{
l = mid + 1;
continue;
}

if (a[i] * b[mid] > x)
{
r = mid - 1;
continue;
}
}

sum += r + 1;

}

return sum;
}
};
``````