Skip to main content
2 of 2
added 14 characters in body
Rby
  • 3
  • 4

Find all subsets with a given sum

How to choose from a set of positive numbers all the subsets that sum to some number x? For example if the set $S=[1,1,2,3,4,5,6,7]$ and I'm searching for all the subsets that sum to $7$ I would have $[1,6],[1,6],[2,5],[1,1,5],[3,4],[1,2,4],[1,2,4],[7]$. What I'm trying to do now is to generate all the possible tuples of $\{0,1\}$, so for the set $S$ which has length $7$ I have: $\{0, 0, 0, 0, 0, 0, 0\}, \{0, 0, 0, 0, 0, 0, 1\}, \{0, 0, 0, 0, 0, 1, 0\},...$, multiply the element of tuples of $0$ and $1$ by the corresponding element of the set $S$ and check if the sum is equal to $7$.

I got the idea of the tuples in this question: N subsets with a given sum? However that problem is slightly different from mine so I was wondering if there's a way to speed things up.

Rby
  • 3
  • 4