Kth Smallest Product of Two Sorted Arrays - Problem

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

example_1.py โ€” Basic Case
$ Input: nums1 = [2, 5], nums2 = [3, 4], k = 2
โ€บ Output: 8
๐Ÿ’ก Note: All possible products: [2ร—3=6, 2ร—4=8, 5ร—3=15, 5ร—4=20]. Sorted: [6, 8, 15, 20]. The 2nd smallest is 8.
example_2.py โ€” With Negatives
$ Input: nums1 = [-4, -2, 0, 3], nums2 = [2, 4], k = 6
โ€บ Output: 0
๐Ÿ’ก Note: Products: [-16, -8, -8, -4, 0, 0, 6, 12]. Sorted: [-16, -8, -8, -4, 0, 0, 6, 12]. The 6th smallest is 0.
example_3.py โ€” All Negative Products
$ Input: nums1 = [-2, -1], nums2 = [1, 2], k = 1
โ€บ Output: -4
๐Ÿ’ก Note: Products: [-2, -4, -1, -2]. Sorted: [-4, -2, -2, -1]. The 1st smallest (minimum) is -4.

Visualization

Tap to expand
Binary Search Strategy VisualizationSearch Space: All Possible Product ValuesMinMaxMidCount Products โ‰ค MidUse sorted arrays tocount efficientlyCompare with kIf count โ‰ฅ k: search leftIf count < k: search right๐ŸŽฏ Key: We never generate all products, just count them smartly!
Understanding the Visualization
1
Establish Boundaries
Find the minimum and maximum possible products from corner values
2
Make Smart Guess
Binary search picks the middle value as our candidate answer
3
Count Efficiently
Use sorted array properties to quickly count products โ‰ค our guess
4
Narrow Down
Based on count vs k, eliminate half of the search space
5
Repeat Until Found
Continue until we find the exact kth smallest product
Key Takeaway
๐ŸŽฏ Key Insight: Binary search on answer space + efficient counting beats generating all products, reducing complexity from O(nm log(nm)) to O((n+m) log(range))

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n*m*log(n*m))

O(n*m) to generate all products plus O(n*m*log(n*m)) to sort them

n
2n
โšก Linearithmic
Space Complexity
O(n*m)

Need to store all n*m products in memory

n
2n
โšก Linearithmic Space

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
Asked in
Google 45 Meta 35 Amazon 28 Microsoft 22
28.8K Views
Medium-High Frequency
~35 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen