Find count of all combination of numbers whose sum is x

575 Views Asked by At

I want to find the sum of all combination of numbers whose sum is x, for e.g.

when x = 3 f(x) = countOf(111,12,21,3) = 4

1

There are 1 best solutions below

3
On BEST ANSWER

Place $x$ balls in a row. Then, between each pair of adjacent balls, either place a divider or don't.

Ex: With 3 balls, you can have:

O|O|O (111)

O|OO (12)

OO|O (21)

OOO (3)

Clearly, there is a 1:1 correspondence between ball/divider arrangements and combinations of numbers whose sum is $x$.

Since there are $x-1$ slots for dividers, there are $2^{x-1}$ ways to place dividers (in each slot, either place a divider or don't). Hence $f(x) = 2^{x-1}$.