K Max Sum Combinations
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.
Input Format :
The first line of input contains an integer 'T', denoting the number of test cases.
The first line of every test case contains two integers ‘N’ and ‘K’ denoting the length of the array/list and the number of required sum combinations respectively.
The second line of every test case contains ‘N’ space-separated integers denoting the elements of the array ‘A’.
The third line of every test case contains ‘N’ space-separated integers denoting the elements of the array ‘B’.
Output Format :
For every test case, return the maximum ‘K’ valid sum combinations in descending order.
Note :
You don’t have to print anything; it has already been taken care of. Just implement the function.
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
CodingNinjas
author
2y
Brute
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 su...read more
CodingNinjas
author
2y
Using Heap
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 ide...read more
Help your peers!
Add answer anonymously...
Top DE Shaw Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app