Largest subset with max sum less than or equals to k
Anonymous User
4147

I have came across with interesting problem. Its a modification of subset sum problem.

For a given array of positive integers,
Problem is find the set of max number of elements having the maximum sum which is <= k.

Eg. 1.

Arr = [1,2,3,4,5,6,7,8,9,10], k = 20

output: [1, 2, 3, 4, 10]
explaination: sum of 1,2,3,4,10 is  20 which is <= k

there are other possible outputs such as

[1, 2, 3, 5, 9], [1, 2, 3, 6, 8], [1, 2, 4, 5, 8], [1, 2, 4, 6, 7], [1, 3, 4, 5, 7] 

all above will be considered as correct answer because sum of all those are 20 which is <= k.
There are other possible set which will be considered as incorrect such as [1,9,10] even though the sum is 20 the number of element is less than other subsets i.e. [1, 2, 3, 4, 10]

Eg. 2

Arr = [10,2,5,8], k = 21

output: [10, 2, 8] 

Constraints:
1 <= arr.length <= 50
1 <= arr[i] <= k <= 10^5
1 <= k <= 10^5

Can anyone help me solve this problem with minimal time & space complexity?

Comments (7)