Maximum Sum Combination
From arrays A and B, return the k largest sums A[i] + B[j] over all pairs.
Intuition
Sort both descending: A[0]+B[0] is the biggest sum. Each popped pair (i,j) has exactly two 'next best' candidates, (i+1,j) and (i,j+1) — a max-heap frontier explores the n² grid of sums best-first, touching only ~k cells.
Both arrays sorted descending means the best pair is (0, 0), and the next best is always a neighbour of something already taken. That's the k-way frontier pattern: keep a heap of candidates, pop the best, and push only the cells adjacent to it. The seen set is essential because two different pops can reach the same cell.
Approach
Best-first over a sum matrix
Imagine the (i,j) grid of sums, sorted axes make it decrease right and down. The k largest live in a staircase near the corner.
Heap + visited set
Pop the max, emit it, push its two neighbours if unseen. The visited set stops (i+1,j) arriving twice via different parents.
Why it's fast
Each emit pushes ≤ 2 nodes → heap stays O(k); total O(k log k) instead of n² sums.
Solution & live demo
Common pitfalls
Building all n×m sums
sums = sorted((x + y for x in a for y in b), reverse=True) return sums[:k]
heap = [(-(a[0] + b[0]), 0, 0)] # expand only the frontier
That's O(nm log nm) work and memory for the k largest values. The heap only ever holds the frontier, so the cost is O(k log k) regardless of how large the arrays are.
Omitting the visited 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, ...)Cell (1, 1) is reachable from both (0, 1) and (1, 0), so without the guard the same sum is pushed twice and appears twice in the output. The set makes each cell enter the heap exactly once.
Forgetting to negate for a max-heap
heap = [(a[0] + b[0], 0, 0)]
heap = [(-(a[0] + b[0]), 0, 0)]
heapq only pops minimums, so storing raw sums yields the k smallest combinations. Values are negated going in and negated again coming out.
Edge cases
Just A[0]+B[0] after sorting — one pop.
Different (i,j) cells may tie; both count, the set dedupes cells not values.