Given a set $S$, find any $N$ numbers than sum to $X$

312 Views Asked by At

Similar but different from the problem here.

I have an unsorted set $S$ of real numbers, and need to sum elements from $S$ to find the real number $X$; However, It could be from $1$ to $N$ elements from that set (I could probably get away with capping the combination portion at $10$ or so if that helps).

I solved this originally by simply summing every possible combination, and stopping when a match was found. This solution is $O(2^N)$, where $N$ is the size of set $S$; which is incredibility undesirable. It froze MS Excel at about $20$ entries, or $N = 20$. I have been spinning my wheels trying to come up with something faster.

Any suggestions??

1

There are 1 best solutions below

2
On BEST ANSWER

This is the subset sum problem, which is a well-known NP-complete problem even if all your numbers are integers.

So in the general case you shouldn't hope to find an algorithm with better than exponential running time.