Maximum Sum Combination
Maximum Sum Combination is a GFG problem (Medium). You are given two integer arrays a and b of the same length n, and an integer k.
- A sum combination is
a[i] + b[j]for one index pair(i, j); each pair is used at most once. - Return the top
ksums, largest first. Equal sums from different pairs are listed separately. ngoes up to 10⁵, so there are up to 10¹⁰ pairs. Building them all is far too slow.
- 1 <= n <= 10⁵ (a and b have the same length)
- 1 <= a[i], b[i] <= 10⁴
- 1 <= k <= n
Intuition
Sort both arrays from largest to smallest and picture every sum in a grid: row i is a[i], column j is b[j], and cell (i, j) holds a[i] + b[j].
- Biggest: the top-left cell
(0, 0)holds the largest sum. - Order: sums shrink as you move right or down, because each step swaps in a smaller number.
- Next candidates: so after taking a cell, the only new cells that can be next are the one below it and the one to its right.
A max-heap holds these candidate cells and always gives back the largest. Each of the k pops adds at most two cells, so only about 2k of the n² cells are ever looked at.
Use this frontier-with-a-heap idea when you need the k best (or k smallest) values from something sorted along two directions: the k largest pair sums of two arrays, a row- and column-sorted matrix, or k pairs with the smallest sums.
Approach
Before reading on: after sorting both arrays in descending order, which pair gives the largest sum? Once you take it, which two pairs could give the second largest? What goes wrong if two different cells both point to the same next cell?
Sort both arrays in descending order
Now a[0] + b[0] is the largest sum, and every sum gets smaller as i or j grows. This ordering is what lets the search look only at neighbours instead of all n² pairs.
Start the heap at the corner
Push (a[0] + b[0], 0, 0) onto a max-heap and add (0, 0) to a seen set.
Pop the largest sum
Pop the top of the heap and append its sum to the result. It is the largest sum not yet taken: every cell not looked at yet lies below or to the right of some cell in the heap, so it cannot be bigger.
Push the cell below and the cell to the right
For (i + 1, j) and (i, j + 1):
- skip it if the index runs past the end of the array;
- skip it if it is already in
seen; - otherwise add it to
seenand push its sum.
Most cells sit next to two taken cells, one above and one to the left, so seen makes sure each cell enters the heap only once.
Stop after k pops
The result already holds the k largest sums in order from largest to smallest, so return it.
Code in Python, C++ and Java
while condition ends the loop.while condition ends the loop.seen check skips it.Common pitfalls
Building every sum first
sums = sorted((x + y for x in a for y in b), reverse=True) return sums[:k]
heap = [(-(a[0] + b[0]), 0, 0)] # grow only the frontier
With n = 10⁵ that is 10¹⁰ sums, far beyond the time and memory limits. The heap touches only about 2k cells.
Leaving out the seen set
for ni, nj in ((i + 1, j), (i, j + 1)):
heapq.heappush(heap, (-(a[ni] + b[nj]), ni, nj))if (ni, nj) not in seen:
seen.add((ni, nj))
heapq.heappush(heap, (-(a[ni] + b[nj]), ni, nj))Cell (1, 1) is the neighbour of both (0, 1) and (1, 0). Without the check it is pushed twice and its sum appears twice in the answer.
Storing raw sums in Python's heapq
heap = [(a[0] + b[0], 0, 0)]
heap = [(-(a[0] + b[0]), 0, 0)]
heapq always pops the smallest item, so raw sums come out smallest first. Negate on the way in and again on the way out.
Edge cases
Each pair counts once, so equal sums are all listed: in a = [1, 4, 2, 3], b = [2, 5, 1, 6] the answer for k = 3 is [10, 9, 9]. The seen set stores index pairs, not sums, so it never drops a real tie.
Complexity
Related heap-on-a-grid problems
The same frontier idea solves all three; the direction and what you return change.
| Problem | Wants | Start cell | Heap |
|---|---|---|---|
| Maximum Sum Combination (GFG) | k largest sums a[i] + b[j] | both arrays' largest | max-heap |
| Find K Pairs with Smallest Sums (LeetCode 373) | k smallest pairs, as pairs | both arrays' smallest | min-heap |
| Kth Smallest Element in a Sorted Matrix (LeetCode 378) | one value: the kth smallest | top-left of the matrix | min-heap |
Maximum Sum Combination FAQ
What are the steps?
- Sort: both arrays in descending order.
- Start: push
(a[0] + b[0], 0, 0)onto a max-heap and mark(0, 0)seen. - Repeat k times: pop the largest, add it to the answer, push
(i + 1, j)and(i, j + 1)if they are in range and unseen. - Complexity: O(n log n + k log k) time, O(k) extra space.
Is this the same as the InterviewBit Maximum Sum Combinations problem?
Yes. InterviewBit's version asks for the C largest sums from two arrays of size N, which is the same task with k renamed to C. The same heap solution applies.