0
$\begingroup$

I am looking for the number of possible outcomes given to a set of numbers X that sum to Y. This is the same issue as here. However, I would like to consider that (i) the numbers can't be repeated and (ii) the order doesn't matter. For example, the only possible outcome for:

  • 4: [3,1] given the list [3,2,1]
  • 5: [[4,1],[3,2]] given the list [4,3,2,1]

An implementation in Python with the possible answers is given here (Answer 2), If a list with ascending or descending order without repetitions is passed the problem is the same as the examples above. However, what I want is just the possible number of answers.

$\endgroup$
4
  • $\begingroup$ Let $S$ is set of numbers of cardinality $n$, $X_i$ are elements of set $S$, such that $X_1>X_2 > \ldots > X_n$, let $S_i$ is set of $X_j$ with $j>i$. Then $N(Y,S)=\sum_{i=1}^{n} N(Y-X_i,S_i)$, $N(0,\emptyset)=1$, $N(Y,\emptyset)=0$ for $Y>0$. $\endgroup$ Commented May 11, 2022 at 9:36
  • $\begingroup$ Maybe, for numerical calculations it is better to use reverse indexing. Let $S_n$ is set of numbers of cardinality $n$, $X_i$ are elements of $S_n$ such that $0 < X_1 < X_2<\ldots X_n$, let $S_i$ is set of $X_j$ with $j\leq i$, $S_0=\emptyset$. Then $N(Y,S_n)=\sum_{i=1}^n N(Y-X_i,S_{i-1})$, $N(0,\emptyset)=1$, $N(Y,\emptyset)=0$ for $Y>0$. $\endgroup$ Commented May 11, 2022 at 9:43
  • $\begingroup$ To the best of my knowledge, there are two distinct viable (analytical) approaches, both of which are discussed in this answer. In the linked answer, there is the trivial difference of the solutions being in strictly ascending order. The real challenge, which is addressed in the linked answer, is eliminating solutions where two or more of the variables have the same value. $\endgroup$ Commented May 11, 2022 at 10:41
  • $\begingroup$ Re my previous comment, to a certain extent, this is different from the OP's problem in that I am specifically assuming that if the desired sum is $T$, that the set in question is $\{0,1,2,\cdots,T\}.$ However, it may be feasible to convert a problem like find all solutions where the set is $\{1,3,4,5\}$ to a similar problem where the adjusted set is $\{0,1,2,3\}.$ $\endgroup$ Commented May 11, 2022 at 17:54

1 Answer 1

0
$\begingroup$

I'm afraid that cannot be computed easily. Even deciding if the number of solutions is greater than 0 is known as the subset sum problem which is NP-complete, so finding the exact number of solutions is at least as hard.

If the set is small, simply trying all subsets could be a solution, and for larger sets otherwise, there are other algorithms, eg. this.

$\endgroup$
1
  • $\begingroup$ Analytical approaches are possible. See my comment, following the original posting. To a certain extent, this is different from the OP's problem in that I am specifically assuming that if the desired sum is $T$, that the set in question is $\{0,1,2,\cdots,T\}.$ $\endgroup$ Commented May 11, 2022 at 17:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .