Sort the array, count the number of elements, then start summing the elements sequentially until their sum is greater than k or you go through each element, and then subtract 1 from the account if the sum is greater than k
pseudo code:
let k=6
sort the array
[1,2,3,4,5,6,7]
let sum=0
let count=7
for (i=0;i<7;i++) {
sum+=array[i];
if (sum>k)
break;
}
if (sum>k)
i--;
i - maximum number of elements.
source
share