


A : [1, 3]
B : [4, 2]
K: 2
The possible sum combinations can be 5(3 + 2), 7(3 + 4), 3(1 + 2), 5(1 + 4).
The 2 maximum sum combinations are 7 and 5.
The first line contains two integers, ‘N’ and ‘K’, denoting the length of the array/list and the number of required sum combinations respectively.
The second line contains ‘N’ space-separated integers denoting the array ‘A’ elements.
The third line contains ‘N’ space-separated integers denoting the array ‘B’ elements.
Return the maximum ‘K’ valid sum combinations in descending order.
You don’t have to print anything; it has already been handled. Just implement the function.
Generate all the possible sum combinations using two loops. Store the sum of all the sum combinations in an array/list and sort the array/list in descending order.
Finally, return the ‘K’ max sub-combinations from that array/list.
The Steps are as follows :
In this approach, instead of exploring all the possible sum combinations, we will only use the sum-combinations that can be the possible candidates for the ‘K’ max sum combinations.
The idea is to sort both arrays and insert the possible candidates of max sum combinations into the max-heap. We will also use a set to make sure that we are using a sum-combination only once. Now, we will keep removing the max element(max sum combination) and keep inserting the next possible candidate in the max heap until we get ‘K’ max sum combinations. We will also store the indices corresponding to the value so that we will be able to get the next possible candidate pair.
The Steps are as follows :