AlgoMaster Logo

Find K Pairs with Smallest Sums

Last Updated: April 4, 2026

medium
4 min read

Understanding the Problem

We have two sorted arrays and we need to find the k pairs (one element from each array) that produce the smallest sums. The naive reaction is to generate every possible pair, sort them by sum, and take the first k. But with arrays up to 10^5 elements each, that would mean up to 10^10 pairs, which is way too many.

The sorted order of both arrays is the critical detail here. Since both arrays are sorted, the pair (nums1[0], nums2[0]) is guaranteed to have the smallest sum. The second smallest sum must be either (nums1[1], nums2[0]) or (nums1[0], nums2[1]). This pattern of "expanding from the minimum" is exactly what a min-heap handles well. We don't need to look at all pairs, just the ones that could possibly be the next smallest.

Key Constraints:

  • 1 <= nums1.length, nums2.length <= 10^5 → Up to 10^10 total pairs. We absolutely cannot enumerate all pairs. We need a way to find the k smallest without generating the rest.
  • 1 <= k <= 10^4 → k is relatively small compared to the total number of pairs. This means we only need to explore a tiny fraction of the pair space.
  • Both arrays are sorted → This is the key property to exploit. Pairs involving smaller indices tend to have smaller sums, so we can explore them in order.
  • -10^9 <= nums1[i], nums2[j] <= 10^9 → Sums can range from -210^9 to 210^9. Use long/int64 if needed to avoid overflow in some languages.

Approach 1: Brute Force (Generate All Pairs)

Intuition

The most straightforward approach: generate every possible pair, calculate their sums, sort all pairs by sum, and take the first k. This is the natural "try everything" starting point.

Since both arrays are sorted, we know the pairs exist. We just need to find the k smallest. The brute force way is to not leverage the sorted property at all, treat it as a general "find top k" problem.

Algorithm

  1. Create an empty list to store all pairs.
  2. For each element nums1[i], pair it with every element nums2[j].
  3. Sort all pairs by their sum.
  4. Return the first k pairs from the sorted list.

Example Walkthrough

1Start: pair nums1[0]=1 with all of nums2
0
1
i
1
7
2
11
1/5

Code

We're generating all m * n pairs even though we only need k of them. Both arrays are sorted, so can we explore pairs in sum order and stop after finding k?

Approach 2: Min-Heap (Optimal)

Intuition

Here's the key insight: think of this as a grid problem. If we lay out a matrix where cell (i, j) represents the sum nums1[i] + nums2[j], both rows and columns are sorted. The smallest value is always at (0, 0). Once we pick (0, 0), the next smallest must be either (1, 0) or (0, 1).

This is exactly the same pattern as "merge k sorted lists." Each row i in the matrix forms a sorted list: nums1[i] + nums2[0], nums1[i] + nums2[1], nums1[i] + nums2[2], ... We're merging these sorted lists and pulling the k smallest elements.

The trick to avoid duplicates is this: seed the min-heap with the first element from each "list" (i.e., (i, 0) for each row i), and when we pop a pair (i, j), we only push (i, j+1). Since we only move forward in nums2 for a given nums1 index, every pair is generated exactly once.

We also cap the initial seeding at min(k, len(nums1)) rows, because we'll never need more than k pairs total, so there's no point starting with more than k rows.

Algorithm

  1. Initialize a min-heap. Push (nums1[i] + nums2[0], i, 0) for i from 0 to min(k, len(nums1)) - 1.
  2. Initialize an empty result list.
  3. While the result has fewer than k pairs and the heap is not empty:

a. Pop the smallest sum pair (sum, i, j) from the heap.

b. Add [nums1[i], nums2[j]] to the result.

c. If j + 1 < len(nums2), push (nums1[i] + nums2[j+1], i, j+1) into the heap.

  1. Return the result.

Example Walkthrough

1Seed heap: (1+2=3, i=0,j=0), (7+2=9, i=1,j=0), (11+2=13, i=2,j=0)
3min913
1/5

Code