ExamAdda Logo

Find K Pairs with Smallest Sums

Easy

You are given two integer arrays nums1 and nums2 sorted in ascending order, and an integer k.

A pair (u, v) consists of one element from nums1 and one element from nums2.

Return the k pairs (u, v) with the smallest sums.

The answer may contain fewer than k pairs if there are not enough possible pairs.

Example 1

Input

m = 3
n = 3
k = 3
nums1 = [1, 7, 11]
nums2 = [2, 4, 6]

Output

[1, 2]
[1, 4]
[1, 6]

Explanation

The three pairs with the smallest sums are:

[1, 2] → 3

[1, 4] → 5

[1, 6] → 7

Example 2

Input

m = 2
n = 3
k = 2
nums1 = [1, 1]
nums2 = [1, 2, 3]

Output

[1, 1]
[1, 1]

Explanation

The two smallest pair sums are both 2.

Constraints

1 <= nums1.length
nums2.length <= 10^5
-10^9 <= nums1[i]
nums2[i] <= 10^9
nums1 and nums2 are sorted in ascending order.
1 <= k <= 10^4
The total number of possible pairs can be larger than k.

Hints:

Hint 1

Since both arrays are sorted, you don't need to generate all possible pairs.

Hint 2

Use a min-heap containing the smallest available pair from each starting position in nums1.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 22, 2026

Last updated

Aug 22, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output