Update appNew update is available. Click here to update.

K Max Sum Combinations

Contributed by
Deep Mavani
Last Updated: 23 Feb, 2023
Easy
yellow-spark
0/40
Avg time to solve 10 mins
Success Rate 90 %
Share
27 upvotes

Problem Statement

You are given two arrays/lists ‘A’ and ‘B’ of size ‘N’ each. You are also given an integer ‘K’. You have to find the ‘K’ maximum and valid sum combinations from all the possible sum combinations of the arrays/lists ‘A’ and ‘B’.

Sum combination is made by adding one element from array ‘A’ and another element from array ‘B’.

For example :
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. 
Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 5
1 <= N <= 100
1 <= K <= N
-10^5 <= A[i], B[i] <= 10^5

Where 'A[i]' and 'B[i]' denotes the ith element in the given arrays/lists. 

Time limit: 1 sec
Sample Input 1 :
2
3 2
1 3 5
6 4 2
1 1
3
4
Sample Output 1 :
11 9
7
Explanation of Sample Output 1 :
In test case 1, for the given arrays/lists, all the possible sum combinations are: 
7(1 + 6), 5(1 + 4), 3(1 + 2), 9(3 + 6), 7(3 + 4), 5(3 + 2), 11(6 + 5), 9(5 + 4), 7(5 + 2).

The two maximum sum combinations from the above combinations are 11 and 9. 

In test case 2, only one pair is possible with sum 7(3 + 4) from the given arrays/lists.
Sample Input 2 :
2
2 2
1 1
1 1
4 1
1 2 3 4
4 3 2 1
Sample Output 2 :
2 2
8
Explanation of sample input 2 :
In test case 1, for the given arrays/lists, two possible sum combinations are : 2(1 + 1), 2(1 + 1).

In test case 2, for the given arrays/lists, one possible sum combination is: 8(4 + 4).
Reset Code
Full screen
Auto
copy-code
Console