All problems
0977HardArrayBinary Search

The k-th Smallest Product of Two Lists

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2040Kth Smallest Product of Two Sorted Arrays

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a 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)
September 7
Apply