


Your are given ‘arr’ = [1, 3, 5, 9], and ‘K’ = 16, we can take the subset of elements [9, 5 ,1] which sums up to 15. Hence the answer is 15.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘K’, denoting the number of elements and the given integer.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the array.
For each test case, print a single integer representing the maximum subset-sum not greater than ‘K.’
Print a separate line for each test case.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 40
0 <= K <= 10^9
0 <= arr[i] <= 10^9
Time Limit: 1 sec
In this approach, we will iterate over all the possible subsets of the given array, and then we will find the maximum sum subset whose sum is not greater than K.
The total number of subsets of an array is 2^N or 1 << N in the bitshift operator.
Algorithm:
In this approach, we will use the meet in the middle search technique, and it is only used when the input is small enough. It divides the array into two sub-arrays, then solves them individually and merges the arrays.
After creating both sub-arrays, we will traverse through one subarray and find the remaining sum in the other subarray. We will do the binary search in the other subarray to find the remaining sum. In the end, we will obtain the maximum subset-sum not greater than K.
Algorithm: