0

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.

  • 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$. – Ivan Kaznacheyeu May 11 '22 at 09:36
  • 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$. – Ivan Kaznacheyeu May 11 '22 at 09:43
  • 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. – user2661923 May 11 '22 at 10:41
  • 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}.$ – user2661923 May 11 '22 at 17:54

1 Answers1

0

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.

jonaslj
  • 320
  • 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}.$ – user2661923 May 11 '22 at 17:52