Show problems as
Classic problem #2040HardArrayBinary Search

Kth Smallest Product of Two Sorted Arrays

Real-world version: The k-th Smallest Product of Two Lists
Tracked in this browser only
Read Kth Smallest Product of Two Sorted Arrays on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Two lists of readings are given as nums1 and nums2, each already in non-decreasing order. Either may hold negative readings.

Take every product of one reading from the first list with one from the second, keeping every combination of positions even when the products repeat. Return the k-th smallest of those products, counting from one.

Examples

Example 1

Input
nums1 = [-4, -2, 0, 3], nums2 = [-5, 1, 6], k = 7
Output
0

The twelve products in order run -24, -12, -10, -6, -5, -2, 0, 0, 0, 3, 18 and 20, so the seventh is nothing.

Example 2

Input
nums1 = [-1], nums2 = [1], k = 1
Output
-1

The only product is a negative reading times a positive one.

Example 3

Input
nums1 = [2, 5], nums2 = [3, 4], k = 2
Output
8

The four products are 6, 8, 15 and 20, so the second is 8.

Constraints

  • 1 <= nums1.length <= 5 * 10^4
  • 1 <= nums2.length <= 5 * 10^4
  • -10^5 <= nums1[i] <= 10^5
  • -10^5 <= nums2[i] <= 10^5
  • 1 <= k <= nums1.length * nums2.length
  • nums1 and nums2 are each in non-decreasing order

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def kth_smallest_product(nums1: list[int], nums2: list[int], k: int) -> int:
Java
public long kthSmallestProduct(int[] nums1, int[] nums2, long k)