Stars and bars counts the ways to split identical objects into distinct groups. Below are 12 practice problems, from the basic formula to upper bounds and digit sums. Every problem has a hint and a full step-by-step solution; try it first, then open the solution.
Contents6 sections
The two formulas you need
Put identical objects into distinct boxes. Write the objects as stars and use bars to separate the boxes. For example, with and :
★★ | ★★★★ | ★ means 2 + 4 + 1 = 7
Every arrangement of the stars and bars is one distribution, so:
- Boxes may be empty (non-negative solutions of ):
- Every box gets at least one (positive solutions): place the bars in the gaps between stars,
Lower bounds are handled by giving the minimum first. Upper bounds need inclusion–exclusion. Both appear below.
Easy problems
Problem 1
In how many ways can 10 identical candies be given to 4 children, if some children may get none?
Hint
Candies are stars, children are boxes. How many bars separate 4 boxes?
View Solution
We need the non-negative solutions of . There are 10 stars and bars, so 13 symbols in a row, and we choose which 3 of them are bars:
Answer: 286
Problem 2
Same 10 candies and 4 children, but every child must get at least one candy. How many ways now?
Hint
Give each child one candy first. How many candies are left to distribute freely?
View Solution
Hand out one candy to each child. That uses 4 candies, and 6 remain with no restriction. Now count non-negative solutions of :
The direct formula gives the same result: .
Answer: 84
Problem 3
How many solutions in positive integers does have?
Hint
“Positive” means each variable is at least 1. Use the second formula.
View Solution
Line up 15 stars. There are 14 gaps between them, and we place 2 bars in two different gaps (no empty group allowed):
Answer: 91
Problem 4
A donut shop sells 6 flavors. How many different boxes of 8 donuts can you buy? (Two boxes are the same if they contain the same number of each flavor.)
Hint
This is choosing with repetition. What are the stars here, and what are the boxes?
View Solution
A box is described by how many donuts of each flavor it holds: with . The 8 donuts are the stars and the 6 flavors are the boxes, so we need 5 bars:
This is the formula for combinations with repetition: choosing items from types gives .
Answer: 1287
Medium problems
Problem 5
Find the number of integer solutions of with , and .
Hint
Substitute and so that every new variable is simply .
View Solution
With , and , the equation becomes
Each solution in gives exactly one solution in , so the count is
Answer: 136
Problem 6
How many non-negative integer solutions does the inequality have?
Hint
Add a fourth variable that takes up the “unused” amount, and the inequality becomes an equation.
View Solution
Introduce a slack variable . Solutions of the inequality match one to one with non-negative solutions of
That is 12 stars and 3 bars:
Answer: 455
Problem 7
After expanding and collecting like terms, how many different terms are there?
Hint
Every term looks like . What do the exponents add up to?
View Solution
Each term is with and , and different exponent triples give different terms. So we count non-negative solutions with 6 stars and 2 bars:
Answer: 28
Problem 8
You roll 4 identical dice and only record which faces came up, not which die showed what. How many different results are possible?
Hint
A result is fixed by how many dice show 1, how many show 2, and so on up to 6.
View Solution
Let be the number of dice showing face . Then with : 4 stars and 5 bars.
Do not confuse this with , which counts ordered results of 4 different dice.
Answer: 126
Problem 9
How many sequences are there with every in ?
Hint
A non-decreasing sequence is fixed once you know how many times each value 1 to 9 appears.
View Solution
Sorting removes the order, so the sequence is just a multiset of 4 values chosen from 9. Let count how often value appears: , . That is 4 stars and 8 bars:
Answer: 495
Hard problems
Problem 10
How many non-negative integer solutions does have if every ?
Hint
Count all solutions, then remove the ones where some variable is 6 or more. Can two variables be 6 or more at the same time?
View Solution
All solutions: .
Bad solutions: if , set . Then , which has solutions. The same holds for and , giving .
Two variables cannot both be , because . So nothing was removed twice, and by inclusion–exclusion:
Answer: 21
Problem 11
How many three-digit numbers have digits that add up to 10?
Hint
The first digit is at least 1, and every digit is at most 9. One of these limits actually removes a case.
View Solution
Write the number as with and . Put :
Without upper limits there are solutions. Since the sum is 9, and can never exceed 9. But must stay at most 8 (because ), and happens in exactly one solution: , which would mean . Removing it:
Answer: 54
Problem 12
Put 15 identical balls into 4 distinct boxes so that every box gets at least 2 balls and box 1 gets at most 5. How many ways are there?
Hint
Handle the lower bounds first, then the single upper bound.
View Solution
Lower bounds: put 2 balls in each box. That uses 8, so 7 remain: , , where .
The condition becomes .
All solutions: .
Bad solutions (): set , so , which has solutions.
Answer: 100
When stars and bars does not apply
- The objects are different (for example, 10 different books into 4 boxes). Each book chooses a box on its own: ways, not a stars and bars count.
- The boxes are identical (for example, splitting 10 into a sum where order does not matter). That is an integer partition problem, which has no simple closed formula.
- Order inside a box matters. Then you are arranging, not just counting how many go where.
Frequently asked questions
Is it or ?
Both. Choosing which of the positions hold bars is the same as choosing which hold stars, and .
Why is it called stars and bars?
The name comes from the picture: objects drawn as stars (★) and dividers drawn as bars (|). The method was popularized by William Feller’s probability textbook.
How do I know if a problem is a stars and bars problem?
Ask two questions: are the objects identical, and are the groups labeled? If both answers are yes, and you only care how many go into each group, stars and bars applies.