How to use stars and bars for solving number of integral solutions to an equality?

How to use the stars and bars method?

Say I want to find number of combinations I can get with $x_1+x_2+x_3+x_4=22$, where $x_i\in\mathbb{N}$.

Is this the correct time to apply the method?

This is being repurposed in an effort to cut down on duplicates, see here:

Coping with abstract duplicate questions

and here: List of abstract duplicates


Yes, the Stars-and-Bars approach works great here, but you should know that there are two "versions" of the Stars-and-Bars approach. In both versions, we look for the number of distinct integer solutions to an equation such as yours.

In the first version, we require that every $x_i$ must be a positive integer.

In the second version, the restriction eases to include all non-negative $x_i$.

So, for example in your case, $x_1= 0, x_2=9, x_3=0, x_4=13$ would be one distinct solution in the second version, but would not be a solution in the first version.

I. positive integers $x_i$

For any pair of positive integers n and k, the number of distinct k-tuples of positive integers whose sum is $n$ is given by the binomial coefficient $${n - 1\choose k-1}.$$

In your case, $k = 4, n=22$. So the number of distinct solutions $(x_1, x_2, x_3, x_4)$ where the $x_i \in \mathbb Z, x_i>0$ is given by $$\binom{22-1}{4-1} = \binom{21}{3} = \frac{21!}{3!18!} = 1330$$


II. non-negative integers $x_i$

For any pair of natural numbers n and k, the number of distinct k-tuples of non-negative integers (which includes the possibility that one or more of the $x_i$ are zero) whose sum is $n$ is given by the binomial coefficient $$\binom{n + k - 1}{n} = \binom{n+k-1}{k-1}.$$

In your problem, $k = 4, n = 22.$ Here, the distinct solutions $(x_1, x_2, x_3, x_4)$ will include those from $I.$, but also allows 4-tuples in which one or more of the $x_i$ are zero: $x_i \in \mathbb Z, x_i\geq 0$.

$$\binom{22 + 4 -1}{22} = \binom{25}{22} = \dfrac{25!}{22!3!} = 2300$$


The star method: consider 22 balls and 3 separations (because you have 4 boxes). I denote $*$ for the balls and $\Big |$ for the separation. Then it's the number of permutation of:

$$\left\{\underbrace{*\ *\ \cdots *\ }_{22\ balls}\Big|\hspace{0.5cm}\Big|\hspace{0.5cm} \Big|\hspace{0.5cm}\right\}$$

There is $25!$ permutations but the permutation of the balls together and the permutations of the separation together doesn't give a new combinaison, so you have to divide $25!$ by $3!22!$ and it gives $$\frac{25!}{3!22!}=\binom{25}{3}$$ different solutions.


How to use the stars and bars method?

For $x_i\ (i=1,2,3,4)\in\mathbb N$, we have $$x_1+x_2+x_3+x_4=22\iff (x_1-1)+(x_2-1)+(x_3-1)+(x_4-1)=18.$$

Here, note that $x_i-1\ (i=1,2,3,4)$ are non-negative integers.

Choosing $4-1=3$ places (for bars) from $18+(4-1)$ places (for bars and stars) leads the answer is $\binom{18+(4-1)}{4-1}=\binom{21}{3}=1330.$