Find K Pairs with Smallest Sums - Problem
Find K Pairs with Smallest Sums

Imagine you're a data analyst tasked with finding the most cost-effective combinations from two sorted price lists! ๐Ÿ’ฐ

You are given two integer arrays nums1 and nums2, both sorted in non-decreasing order, and an integer k. Your mission is to find the k pairs with the smallest sums.

A pair (u, v) consists of one element u from the first array and one element v from the second array. The sum of a pair is simply u + v.

Goal: Return the k pairs (uโ‚, vโ‚), (uโ‚‚, vโ‚‚), ..., (uโ‚–, vโ‚–) with the smallest sums.

Example: If nums1 = [1,7,11] and nums2 = [2,4,6], the pair (1,2) has sum 3, which would be the smallest possible sum.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
โ€บ Output: [[1,2],[1,4],[1,6]]
๐Ÿ’ก Note: The first 3 pairs with smallest sums are: (1,2) with sum 3, (1,4) with sum 5, and (1,6) with sum 7. Since all pairs with nums1[0]=1 have the smallest possible sums, we return these first.
example_2.py โ€” Larger K
$ Input: nums1 = [1,1,2], nums2 = [1,2,3], k = 2
โ€บ Output: [[1,1],[1,1]]
๐Ÿ’ก Note: The first 2 pairs with smallest sums are: (1,1) with sum 2, and another (1,1) with sum 2. Both use the first element from nums1 and the first element from nums2.
example_3.py โ€” K Exceeds Total Pairs
$ Input: nums1 = [1,2], nums2 = [3], k = 3
โ€บ Output: [[1,3],[2,3]]
๐Ÿ’ก Note: Only 2 pairs are possible: (1,3) with sum 4 and (2,3) with sum 5. Since k=3 exceeds the total number of pairs, we return all available pairs.

Constraints

  • 1 โ‰ค nums1.length, nums2.length โ‰ค 105
  • -109 โ‰ค nums1[i], nums2[i] โ‰ค 109
  • nums1 and nums2 both are sorted in non-decreasing order
  • 1 โ‰ค k โ‰ค 104

Visualization

Tap to expand
๐ŸŽฏ Min-Heap Strategy for Finding K Smallest PairsInput Arraysnums1 = [1, 7, 11]nums2 = [2, 4, 6]Step 1: Initialize Heap(1,2)=3(1,4)=5(1,6)=7Min-heap with first row pairsStep 2: Extract & Expand(1,2)=3SELECTED(7,2)=9NEW CANDIDATEHeap Evolution (k=3)Initial:(1,2)=3(1,4)=5(1,6)=7After pop:(1,4)=5(1,6)=7(7,2)=9Result: (1,2)๐ŸŽฏ Final Result: Top 3 Pairs(1,2)(1,4)(1,6)
Understanding the Visualization
1
Start with Cheapest Base
Begin with the cheapest appetizer paired with all main courses - these are your initial candidates
2
Pick Best Available
Always choose the combination with the lowest current total cost
3
Smart Expansion
When you pick a combination, add the next pricier appetizer with the same main course as a new candidate
4
Repeat Until Complete
Continue until you've found k combinations - no need to check all possibilities!
Key Takeaway
๐ŸŽฏ Key Insight: By using a min-heap and leveraging the sorted nature of both arrays, we only explore the most promising combinations, achieving O(k*log(min(k,m))) complexity instead of O(m*n*log(m*n)) for the brute force approach!
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 27 Apple 19
142.4K Views
High Frequency
~18 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen