Find the number of subsets whose sum is less than or equal to the specified number K; Restrictions: the maximum number of elements of the set is n = 40, each should not exceed 10 ^ 16 and most importantly: the work execution time is up to 0.5 seconds and the use of not more than 20MB of RAM
Example :
Enter:
n = 5, s = 1000,
a [n] = {100, 500, 500, 1500, 1000}
Conclusion:
v = 7 + 1 (empty subset) = {{empty}, {100}, {500}, {100 + 500}, {500}, {100 + 500}, {1000}, {500 + 500}}
I tried to solve by simple enumeration of the sums of all possible subsets, but for n> 20 the work execution time grows exponentially.
If anyone can explain how this method works, I will be extremely grateful.
I found the following explanation:
- Split the set of integers into 2 subsets of A and B.
- Find all possible subsets of the number X.Yestand 2. ^ (n / 2).
- Now merge these 2 subproblems. Finds from x and y array of what their sum is less than or equal to S.
- It is easy to iterate over all the elements of the array. This will take O ((2n / 2) 2) which is equivalent to O (2n).
- For example, it makes it possible to make it less complex, to make it less complex, to make it less complex
- Binary search here helps in reducing complexity from 2nto 2n / 2log (2n / 2) which is equivalent to 2n / 2n.
- Thus our final running time is O (2n / 2n).
Did not quite understand the third point.