Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
nums1 and nums2 as well as 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 1:
Input: nums1 = [2,5], nums2 = [3,4], k = 2 Output: 8 Explanation: The 2 smallest products are: - nums1[0] * nums2[0] = 2 * 3 = 6 - nums1[0] * nums2[1] = 2 * 4 = 8 The 2nd smallest product is 8.
Example 2:
Input: nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6 Output: 0 Explanation: The 6 smallest products are: - nums1[0] * nums2[1] = (-4) * 4 = -16 - nums1[0] * nums2[0] = (-4) * 2 = -8 - nums1[1] * nums2[1] = (-2) * 4 = -8 - nums1[1] * nums2[0] = (-2) * 2 = -4 - nums1[2] * nums2[0] = 0 * 2 = 0 - nums1[2] * nums2[1] = 0 * 4 = 0 The 6th smallest product is 0.
Example 3:
Input: nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3 Output: -6 Explanation: The 3 smallest products are: - nums1[0] * nums2[4] = (-2) * 5 = -10 - nums1[0] * nums2[3] = (-2) * 4 = -8 - nums1[4] * nums2[0] = 2 * (-3) = -6 The 3rd smallest product is -6.
Constraints:
1 <= nums1.length, nums2.length <= 5 * 104-105 <= nums1[i], nums2[j] <= 1051 <= k <= nums1.length * nums2.lengthnums1 and nums2 are sorted.Problem summary: Given two sorted 0-indexed integer arrays nums1 and nums2 as well as 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[2,5] [3,4] 2
[-4,-2,0,3] [2,4] 6
[-2,-1,0,1,2] [-3,-1,2,4,5] 3
find-k-pairs-with-smallest-sums)k-diff-pairs-in-an-array)maximum-number-of-robots-within-budget)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2040: Kth Smallest Product of Two Sorted Arrays
class Solution {
private int[] nums1;
private int[] nums2;
public long kthSmallestProduct(int[] nums1, int[] nums2, long k) {
this.nums1 = nums1;
this.nums2 = nums2;
int m = nums1.length;
int n = nums2.length;
int a = Math.max(Math.abs(nums1[0]), Math.abs(nums1[m - 1]));
int b = Math.max(Math.abs(nums2[0]), Math.abs(nums2[n - 1]));
long r = (long) a * b;
long l = (long) -a * b;
while (l < r) {
long mid = (l + r) >> 1;
if (count(mid) >= k) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
private long count(long p) {
long cnt = 0;
int n = nums2.length;
for (int x : nums1) {
if (x > 0) {
int l = 0, r = n;
while (l < r) {
int mid = (l + r) >> 1;
if ((long) x * nums2[mid] > p) {
r = mid;
} else {
l = mid + 1;
}
}
cnt += l;
} else if (x < 0) {
int l = 0, r = n;
while (l < r) {
int mid = (l + r) >> 1;
if ((long) x * nums2[mid] <= p) {
r = mid;
} else {
l = mid + 1;
}
}
cnt += n - l;
} else if (p >= 0) {
cnt += n;
}
}
return cnt;
}
}
// Accepted solution for LeetCode #2040: Kth Smallest Product of Two Sorted Arrays
func kthSmallestProduct(nums1 []int, nums2 []int, k int64) int64 {
m := len(nums1)
n := len(nums2)
a := max(abs(nums1[0]), abs(nums1[m-1]))
b := max(abs(nums2[0]), abs(nums2[n-1]))
r := int64(a) * int64(b)
l := -r
count := func(p int64) int64 {
var cnt int64
for _, x := range nums1 {
if x > 0 {
l, r := 0, n
for l < r {
mid := (l + r) >> 1
if int64(x)*int64(nums2[mid]) > p {
r = mid
} else {
l = mid + 1
}
}
cnt += int64(l)
} else if x < 0 {
l, r := 0, n
for l < r {
mid := (l + r) >> 1
if int64(x)*int64(nums2[mid]) <= p {
r = mid
} else {
l = mid + 1
}
}
cnt += int64(n - l)
} else if p >= 0 {
cnt += int64(n)
}
}
return cnt
}
for l < r {
mid := (l + r) >> 1
if count(mid) >= k {
r = mid
} else {
l = mid + 1
}
}
return l
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2040: Kth Smallest Product of Two Sorted Arrays
class Solution:
def kthSmallestProduct(self, nums1: List[int], nums2: List[int], k: int) -> int:
def count(p: int) -> int:
cnt = 0
n = len(nums2)
for x in nums1:
if x > 0:
cnt += bisect_right(nums2, p / x)
elif x < 0:
cnt += n - bisect_left(nums2, p / x)
else:
cnt += n * int(p >= 0)
return cnt
mx = max(abs(nums1[0]), abs(nums1[-1])) * max(abs(nums2[0]), abs(nums2[-1]))
return bisect_left(range(-mx, mx + 1), k, key=count) - mx
// Accepted solution for LeetCode #2040: Kth Smallest Product of Two Sorted Arrays
impl Solution {
pub fn kth_smallest_product(nums1: Vec<i32>, nums2: Vec<i32>, k: i64) -> i64 {
let m = nums1.len();
let n = nums2.len();
let a = nums1[0].abs().max(nums1[m - 1].abs()) as i64;
let b = nums2[0].abs().max(nums2[n - 1].abs()) as i64;
let mut l = -a * b;
let mut r = a * b;
let count = |p: i64| -> i64 {
let mut cnt = 0i64;
for &x in &nums1 {
if x > 0 {
let mut left = 0;
let mut right = n;
while left < right {
let mid = (left + right) / 2;
if (x as i64) * (nums2[mid] as i64) > p {
right = mid;
} else {
left = mid + 1;
}
}
cnt += left as i64;
} else if x < 0 {
let mut left = 0;
let mut right = n;
while left < right {
let mid = (left + right) / 2;
if (x as i64) * (nums2[mid] as i64) <= p {
right = mid;
} else {
left = mid + 1;
}
}
cnt += (n - left) as i64;
} else if p >= 0 {
cnt += n as i64;
}
}
cnt
};
while l < r {
let mid = l + (r - l) / 2;
if count(mid) >= k {
r = mid;
} else {
l = mid + 1;
}
}
l
}
}
// Accepted solution for LeetCode #2040: Kth Smallest Product of Two Sorted Arrays
function kthSmallestProduct(nums1: number[], nums2: number[], k: number): number {
const m = nums1.length;
const n = nums2.length;
const a = BigInt(Math.max(Math.abs(nums1[0]), Math.abs(nums1[m - 1])));
const b = BigInt(Math.max(Math.abs(nums2[0]), Math.abs(nums2[n - 1])));
let l = -a * b;
let r = a * b;
const count = (p: bigint): bigint => {
let cnt = 0n;
for (const x of nums1) {
const bx = BigInt(x);
if (bx > 0n) {
let l = 0,
r = n;
while (l < r) {
const mid = (l + r) >> 1;
const prod = bx * BigInt(nums2[mid]);
if (prod > p) {
r = mid;
} else {
l = mid + 1;
}
}
cnt += BigInt(l);
} else if (bx < 0n) {
let l = 0,
r = n;
while (l < r) {
const mid = (l + r) >> 1;
const prod = bx * BigInt(nums2[mid]);
if (prod <= p) {
r = mid;
} else {
l = mid + 1;
}
}
cnt += BigInt(n - l);
} else if (p >= 0n) {
cnt += BigInt(n);
}
}
return cnt;
};
while (l < r) {
const mid = (l + r) >> 1n;
if (count(mid) >= BigInt(k)) {
r = mid;
} else {
l = mid + 1n;
}
}
return Number(l);
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.