githubEdit

27. Find K Smallest Sum Pairs

βœ… GFG solution to Find K Smallest Sum Pairs problem: efficiently find k pairs with smallest sums from two sorted arrays using optimized heap approach. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

You are given two integer arrays arr1[] and arr2[] sorted in ascending order and an integer k. Your task is to find k pairs with the smallest sums, such that one element of each pair belongs to arr1[] and the other belongs to arr2[].

Return the list of these k pairs, where each pair is represented as [arr1[i], arr2[j]].

πŸ“˜ Examples

Example 1

Input: arr1[] = [1, 7, 11], arr2[] = [2, 4, 6], k = 3
Output: true
Explanation: All possible combinations of elements from the two arrays are:
[1, 2], [1, 4], [1, 6], [7, 2], [7, 4], [7, 6], [11, 2], [11, 4], [11, 6]. 
Among these, the three pairs with the minimum sums are [1, 2], [1, 4], [1, 6].

Example 2

Input: arr1[] = [1, 3], arr2[] = [2, 4] k = 2
Output: true
Explanation: All possible combinations are [1, 2], [1, 4], [3, 2], [3, 4]. 
Among these, the two pairs with the minimum sums are [1, 2], [3, 2].

πŸ”’ Constraints

  • $1 \le \text{arr1.size()}, \text{arr2.size()} \le 5 \times 10^4$

  • $1 \le \text{arr1}[i], \text{arr2}[j] \le 10^9$

  • $1 \le k \le 10^3$

βœ… My Approach

The optimal approach uses a Min-Heap (Priority Queue) with an intelligent initialization strategy to efficiently find k smallest sum pairs:

Optimized Row-by-Row Heap Approach

  1. Initialize Min-Heap:

    • Start by pushing the first element of each row in arr1[] paired with arr2[0] into the min-heap.

    • Only push min(arr1.size(), k) initial pairs to optimize space.

    • Each heap entry stores: {sum, index_i, index_j}.

  2. Extract Minimum Pairs:

    • Pop the top element (smallest sum) from the heap.

    • Add the corresponding pair [arr1[i], arr2[j]] to the result.

  3. Expand Window:

    • After extracting pair at indices (i, j), push the next pair from the same row: (i, j+1).

    • This ensures we only explore necessary pairs in sorted order.

  4. Repeat Until k Pairs:

    • Continue extracting and expanding until we have collected k pairs or heap becomes empty.

  5. Key Optimization:

    • By initializing only the first column and expanding row-wise, we avoid exploring all n*m pairs.

    • The sorted property ensures we always get the next smallest sum.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(k*log(min(k,n))), where n is the size of arr1. We perform at most k heap operations, and the heap size is bounded by min(k, n) since we only initialize that many rows. Each heap operation takes O(log(heap_size)) time.

  • Expected Auxiliary Space Complexity: O(min(k,n)), as the heap stores at most min(k, n) elements at any time. This is significantly better than storing all n*m pairs.

πŸ§‘β€πŸ’» Code (C++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Set-Based Heap Approach

πŸ’‘ Algorithm Steps:

  1. Use min-heap with set to track visited indices.

  2. Start with pair (0,0) and expand neighbors.

  3. Track visited pairs to avoid duplicates.

  4. Extract k smallest pairs from heap progressively.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(k*log(k)) - Heap operations for k pairs

  • Auxiliary Space: πŸ’Ύ O(k) - Set and heap storage

βœ… Why This Approach?

  • Prevents duplicate pairs effectively with set

  • Handles edge cases with bounds checking

  • Similar to original provided solution

πŸ“Š 3️⃣ Custom Comparator Heap

πŸ’‘ Algorithm Steps:

  1. Initialize heap with first element from each row paired with arr2[0].

  2. Use custom lambda comparator to compare sums directly.

  3. Extract minimum and add next element from same row.

  4. Continue until k pairs extracted or heap empty.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(k*log(min(k,n))) - Optimized heap size

  • Auxiliary Space: πŸ’Ύ O(min(k,n)) - Limited heap size

βœ… Why This Approach?

  • Most space efficient among heap approaches

  • Custom comparator provides cleaner code

  • Optimal for cases where k is small

πŸ“Š 4️⃣ Brute Force with Sorting

πŸ’‘ Algorithm Steps:

  1. Generate all possible pairs from both arrays.

  2. Store pairs with their sums in a structure.

  3. Sort all pairs based on their sum values.

  4. Return the first k pairs from sorted result.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(nmlog(n*m)) - Generating and sorting all pairs

  • Auxiliary Space: πŸ’Ύ O(n*m) - Storing all pairs

βœ… Why This Approach?

  • Simple implementation without complex data structures

  • Works for any input without additional constraints

  • Good for small input sizes

Note: This approach results in Time Limit Exceeded (TLE) for large inputs (fails ~1010/1112 test cases due to time constraints).

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🏷️ Row-by-Row Heap

🟒 O(k*log(min(k,n)))

🟒 O(min(k,n))

πŸš€ Most space efficient

πŸ”§ Requires sorted arrays

πŸ” Set-Based Heap

🟒 O(k*log(k))

🟑 O(k)

πŸ“– Prevents duplicates

πŸ’Ύ Extra set overhead

πŸ”„ Custom Comparator Heap

🟒 O(k*log(min(k,n)))

🟒 O(min(k,n))

⭐ Clean structured pairs

πŸ”§ Lambda syntax complexity

πŸ“Š Brute Force (TLE)

πŸ”΄ O(nmlog(n*m))

πŸ”΄ O(n*m)

🎯 Simple implementation

🐌 Inefficient for large inputs

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Optimal performance needed

πŸ₯‡ Row-by-Row Heap

β˜…β˜…β˜…β˜…β˜…

πŸ“– Need duplicate prevention

πŸ₯ˆ Set-Based Heap

β˜…β˜…β˜…β˜…β˜†

πŸ”§ Small inputs only

πŸ₯‰ Brute Force (TLE)

β˜…β˜…β˜…β˜†β˜†

🎯 Interview/Competitive

πŸ… Custom Comparator Heap

β˜…β˜…β˜…β˜…β˜…

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated