Solution: K Maximum Sum Combinations From Two Arrays
Let’s solve the K Maximum Sum Combinations From Two Arrays using the Top K Elements pattern.
We'll cover the following
Statement
You are given two integer arrays, arr1
and arr2
, each of size 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
Note: The output should be sorted in non-increasing order.
Constraints:
arr1[i]
,arr2[i]
k
Solution
The essence of the solution lies in identifying the top k
maximum sum combinations of both input arrays using the top k elements pattern. Instead of generating all possible sum combinations (which would result in a quadratic time complexity), the solution uses a max heap to dynamically compute the largest sum combinations. The solution begins by sorting both input arrays in descending order. This ensures that the largest sums can be calculated by combining elements from the beginning of both arrays. The max heap stores these combinations along with their index pairs, while a visited set is used to prevent processing duplicate index pairs.
The key operation involves extracting the largest sum from the heap and then considering the next two potential sum combinations: arr1
while keeping the element in arr2
fixed, and arr2
while keeping the element in arr1
fixed. These two combinations are pushed into the heap, but only if the index pairs k
times, the solution extracts the largest sums and continues to explore the next best combinations until k
maximum sums are found. Once the k
sums are extracted, the result list is returned in non-increasing order.
Now, let’s look at the solution steps below:
We start by sorting arrays,
arr1
andarr2
, in descending order.We initialize a max heap,
pq
, to store the possible sum combinations and the indexes of the elements contributing to each sum. The sum of the first elements from both arrays (arr1[0] + arr2[0]
) is pushed into the heap, along with their respective indexes (0, 0
).We use a
visited
set to keep track of the pairs of indexes that have already been processed, preventing duplicate combinations.We also initialize an empty list,
result
, to store the output of the topk
maximum sum combinations..We then iterate
k
times to extract the top k largest sum combinations. In each iteration:We extract the largest sum combination from the heap with its corresponding indexes, and add the sum to
result
.We generate the next possible sum combinations by considering the following:
Moving to the next element in
arr1
while keeping the extracted index ofarr2
(i+1, j
) the same. If this combination is valid (i.e., the index exists and hasn’t been visited before), it’s pushed to the heap and marked as visited.Moving to the next element in
arr2
while keeping the extracted index ofarr1
(i, j+1
) the same. Similarly, if this combination is valid and not yet visited, it’s pushed to the heap and marked as visited.
We continue this process until
k
combinations have been added to theresult
array.Finally, we return the
result
array, containing thek
maximum sum combinations in non-increasing order.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.