Imagine you're given two sorted arrays of integers, nums1 and nums2, and you need to find a very specific value: the kth smallest product that can be formed by multiplying any element from the first array with any element from the second array.
More formally, given two sorted 0-indexed integer arrays nums1 and nums2, and 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.
Example: If nums1 = [2, 5] and nums2 = [3, 4], the possible products are: [6, 8, 15, 20]. The 3rd smallest product would be 15.
Note: This problem becomes particularly challenging when dealing with negative numbers, as they can flip the ordering of products!
Input & Output
Visualization
Time & Space Complexity
O(n*m) to generate all products plus O(n*m*log(n*m)) to sort them
Need to store all n*m products in memory
Constraints
- 1 โค nums1.length, nums2.length โค 5 ร 104
- -105 โค nums1[i], nums2[j] โค 105
- 1 โค k โค nums1.length ร nums2.length
- Both arrays are sorted in non-decreasing order