I would like to know if there exist algorithms that solves this issue. It is little bit similar to knapsack 0-1 problem, or power set problem however it is different.
Given a finite set of sorted real numbers we need to generate all possible subsets whose sum <= k. Here k is real, the sorted real numbers are all positive. For example an array = {1.48, 2.21 3.07, 4.35, 4.46} and k = 5.94 Output is : {4.46}, {4.46, 1.48}, {4.35}, {4.35, 1.48}, {3.07}, {3.07, 2.21}, {2.21}, {2,21, 1.48} and {1.48} .
One way to solve is to simply traverse from highest number {4.46} to see how many you can inlude in the basket then continue going next lowest number {4.35} and so on. Is there an efficient way to do this? let me know
Greedy algorithm can definitely work. To take advantage of the fact that the input is sorted, binary search can be used.
The basic idea is: first search for the biggest number in the array that is smaller than K by binary search, push the result element to a stack, then recursively search the subarray that ends at that element for a sum K – the value of that element. After this is done, search in the subarray for a sum K to cover the situations in which that element is not chosen.
Sample code: