Skip to content

Find K Pairs with Smallest Sums

LeetCode #373 - Find K Pairs with Smallest Sums

Section titled “LeetCode #373 - Find K Pairs with Smallest Sums”

Solve this classic coding interview problem efficiently using multiple approaches.

InputOutputExplanation
[example1]result1Explanation goes here
[example2]result2Explanation goes here
  • Standard LeetCode constraints
  • Refer to the official problem statement for exact details
ApproachTimeSpaceBest When
Approach 1O(n)O(1) or O(n)General case
Approach 2O(n log n)O(n)Alternative
Approach 3O(n²)O(1)Educational
  • Pick Approach 1 if you want the optimal solution.
  • Pick Approach 3 if you are learning the fundamentals.
  • Pick Approach 2 for interview preparation.
Recommended
Approach 1
O(n) time · Minimal space
Alternative
Approach 2
O(n log n) time · More space
Learning
Approach 3
O(n²) time · No extra space
★ Recommended

Use a min heap to efficiently track the k smallest pairs. Start with the pair (nums1[0], nums2[0]) and explore neighboring pairs in increasing order.

⏱ Time O(k log n) k iterations 💾 Space O(n) Heap storage
find_k_pairs_with_smallest_sum_min_heap.py
from typing import List
def find_k_pairs_with_smallest_sum_min_heap(nums: List[int]) -> int:
"""
Find K Pairs With Smallest Sum - Min Heap approach.
Time: O(n), Space: O(1) or O(n) depending on approach
"""
pass
if __name__ == "__main__":
# Test cases
pass
🎯 Interview Favourite

Place one pointer at the end of the first array and one at the beginning of the second array. Adjust pointers based on the pair sum.

⏱ Time O(n log n) Pointer movement 💾 Space O(1) No extra space
find_k_pairs_with_smallest_sum_two_pointer.py
from typing import List
def find_k_pairs_with_smallest_sum_two_pointer(nums: List[int]) -> int:
"""
Find K Pairs With Smallest Sum - Two Pointer approach.
Time: O(n), Space: O(1) or O(n) depending on approach
"""
pass
if __name__ == "__main__":
# Test cases
pass
✓ Simple

Generate all possible pairs and sort them by sum. Return the first k pairs.

⏱ Time O(n² log n) All pairs 💾 Space O(n²) All pairs stored
find_k_pairs_with_smallest_sum_brute_force.py
from typing import List
def find_k_pairs_with_smallest_sum_brute_force(nums: List[int]) -> int:
"""
Find K Pairs With Smallest Sum - Brute Force approach.
Time: O(n), Space: O(1) or O(n) depending on approach
"""
pass
if __name__ == "__main__":
# Test cases
pass