Kth Smallest Product of Two Sorted Arrays

Given two sorted 0-indexed integer arrays nums1 and nums2 as well as an integer k, return the k^th (1-based) smallest product of nums1[i] * nums2[j] where 0 <= i < nums1.length and 0 <= j < nums2.length.

Example 1
Inputnums1 = [2,5], nums2 = [3,4], k = 2
Output8
The two smallest products are 2 * 3 = 6 and 2 * 4 = 8, so the 2nd smallest product is 8.
Example 2
Inputnums1 = [-4,-2,0,3], nums2 = [2,4], k = 6
Output0
The six smallest products are -16, -8, -8, -4, 0, and 0, so the 6th smallest product is 0.

Constraints

  • 1 <= nums1.length, nums2.length <= 5 * 10^4
  • -10^5 <= nums1[i], nums2[j] <= 10^5
  • 1 <= k <= nums1.length * nums2.length
  • nums1 and nums2 are sorted.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate