Search⌘ K
AI Features

Solution: K Maximum Sum Combinations From Two Arrays

Explore how to efficiently find the k largest sums formed by pairing elements from two arrays. This lesson shows you how to use sorting and a max heap to avoid computing all combinations, providing a clear method to track and generate the top sums dynamically. Understand the approach's time and space complexity and learn to apply the top k elements pattern to similar problems.

Statement

You are given two integer arrays, arr1 and arr2, each of size nn. You are also given an integer k. Your task is to return the k largest sum combinations that can be formed by adding one element from arr1 and one element from arr2, for all possible pairs (arr1[i] + arr2[j]), where 0i,j<n0 ≤ i, j < n.

Note: The output should be sorted in non-increasing order.

Constraints:

  • 1n10001 ≤ n ≤ 1000 ...