Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Day 29 : K Max Sum Combinations

Easy
0/40
Average time to solve is 10m
profile
Contributed by
45 upvotes
Asked in companies
WalmartInfo Edge India (Naukri.com)D.E.Shaw

Problem statement

You are given two arrays/lists ‘A’ and ‘B’ of size ‘N’ each. You are also given an integer ‘K’.


You must return ‘K’ distinct maximum and valid sum combinations from all the possible sum combinations of the arrays/lists ‘A’ and ‘B’.


Sum combination adds one element from array ‘A’ and another 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 )
Sample Input 1 :
3 2
1 3 5
6 4 2
Sample Output 1 :
11 9
Explanation of Sample Output 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. 
Sample Input 2 :
2 1
1 1
1 1
Sample Output 2 :
2
Explanation of sample input 2 :
For the given arrays/lists, two possible sum combinations are 2(1 + 1), and 2(1 + 1).
Constraints :
1 <= N <= 100
1 <= K <= N
-10^5 <= A[i], B[i] <= 10^5

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

Time limit: 1 sec
Full screen
Console