Skip to content

2040. Kth Smallest Product of Two Sorted Arrays #1850

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the k-th smallest product of pairs from two sorted arrays. The challenge lies in efficiently handling both negative and non-negative numbers while leveraging the sorted nature of the arrays to avoid a brute-force approach.

Approach

  1. Separate Arrays by Sign:

    • Split each array into two parts: one containing the absolute values of negative numbers (sorted in ascending order) and another containing non-negative numbers (including zeros).
  2. Count Negative Products:

    • Calculate the total number of strictly negative products. This is derived from combinations of negative numbers from one array and non-negative numbers from the other array.
  3. Determine Sign and Adjust k:

    • If k e…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Jun 25, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jun 25, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants