Skip to content

Stars and Bars: 12 Practice Problems with Solutions

Type
Practice problems
Problems
12
Levels
Easy · Medium · Hard

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 nn identical objects into kk distinct boxes. Write the objects as stars and use k−1k-1 bars to separate the boxes. For example, with n=7n=7 and k=3k=3:

★★ | ★★★★ | ★   means   2 + 4 + 1 = 7

Every arrangement of the nn stars and k−1k-1 bars is one distribution, so:

  • Boxes may be empty (non-negative solutions of x1+⋯+xk=nx_1+\dots+x_k=n): (n+k−1k−1)\binom{n+k-1}{k-1}
  • Every box gets at least one (positive solutions): place the k−1k-1 bars in the n−1n-1 gaps between stars, (n−1k−1)\binom{n-1}{k-1}

Lower bounds are handled by giving the minimum first. Upper bounds need inclusion–exclusion. Both appear below.

Easy problems

Problem 1

Easy

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 x1+x2+x3+x4=10x_1+x_2+x_3+x_4=10. There are 10 stars and 4−1=34-1=3 bars, so 13 symbols in a row, and we choose which 3 of them are bars:

(133)=13⋅12⋅113⋅2⋅1=286.\binom{13}{3}=\frac{13\cdot 12\cdot 11}{3\cdot 2\cdot 1}=286.

Answer: 286

Problem 2

Easy

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 y1+y2+y3+y4=6y_1+y_2+y_3+y_4=6:

(6+33)=(93)=84.\binom{6+3}{3}=\binom{9}{3}=84.

The direct formula gives the same result: (n−1k−1)=(93)=84\binom{n-1}{k-1}=\binom{9}{3}=84.

Answer: 84

Problem 3

Easy

How many solutions in positive integers does x+y+z=15x+y+z=15 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):

(142)=91.\binom{14}{2}=91.

Answer: 91

Problem 4

Easy

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: x1+⋯+x6=8x_1+\dots+x_6=8 with xi≥0x_i\ge 0. The 8 donuts are the stars and the 6 flavors are the boxes, so we need 5 bars:

(8+55)=(135)=1287.\binom{8+5}{5}=\binom{13}{5}=1287.

This is the formula for combinations with repetition: choosing rr items from mm types gives (r+m−1r)\binom{r+m-1}{r}.

Answer: 1287

Medium problems

Problem 5

Medium

Find the number of integer solutions of x1+x2+x3=20x_1+x_2+x_3=20 with x1≥2x_1\ge 2, x2≥3x_2\ge 3 and x3≥0x_3\ge 0.

Hint

Substitute y1=x1−2y_1=x_1-2 and y2=x2−3y_2=x_2-3 so that every new variable is simply ≥0\ge 0.

View Solution

With y1=x1−2≥0y_1=x_1-2\ge 0, y2=x2−3≥0y_2=x_2-3\ge 0 and y3=x3y_3=x_3, the equation becomes

y1+y2+y3=20−2−3=15.y_1+y_2+y_3=20-2-3=15.

Each solution in yy gives exactly one solution in xx, so the count is

(15+22)=(172)=136.\binom{15+2}{2}=\binom{17}{2}=136.

Answer: 136

Problem 6

Medium

How many non-negative integer solutions does the inequality x1+x2+x3≤12x_1+x_2+x_3\le 12 have?

Hint

Add a fourth variable that takes up the “unused” amount, and the inequality becomes an equation.

View Solution

Introduce a slack variable s=12−(x1+x2+x3)≥0s=12-(x_1+x_2+x_3)\ge 0. Solutions of the inequality match one to one with non-negative solutions of

x1+x2+x3+s=12.x_1+x_2+x_3+s=12.

That is 12 stars and 3 bars:

(153)=455.\binom{15}{3}=455.

Answer: 455

Problem 7

Medium

After expanding (x+y+z)6(x+y+z)^6 and collecting like terms, how many different terms are there?

Hint

Every term looks like xaybzcx^a y^b z^c. What do the exponents add up to?

View Solution

Each term is xaybzcx^a y^b z^c with a+b+c=6a+b+c=6 and a,b,c≥0a,b,c\ge 0, and different exponent triples give different terms. So we count non-negative solutions with 6 stars and 2 bars:

(82)=28.\binom{8}{2}=28.

Answer: 28

Problem 8

Medium

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 xjx_j be the number of dice showing face jj. Then x1+⋯+x6=4x_1+\dots+x_6=4 with xj≥0x_j\ge 0: 4 stars and 5 bars.

(95)=(94)=126.\binom{9}{5}=\binom{9}{4}=126.

Do not confuse this with 64=12966^4=1296, which counts ordered results of 4 different dice.

Answer: 126

Problem 9

Medium

How many sequences a1≤a2≤a3≤a4a_1\le a_2\le a_3\le a_4 are there with every aia_i in {1,2,…,9}\{1,2,\dots,9\}?

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 cvc_v count how often value vv appears: c1+⋯+c9=4c_1+\dots+c_9=4, cv≥0c_v\ge 0. That is 4 stars and 8 bars:

(124)=495.\binom{12}{4}=495.

Answer: 495

Hard problems

Problem 10

Hard

How many non-negative integer solutions does x1+x2+x3=10x_1+x_2+x_3=10 have if every xi≤5x_i\le 5?

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: (122)=66\binom{12}{2}=66.

Bad solutions: if x1≥6x_1\ge 6, set y1=x1−6y_1=x_1-6. Then y1+x2+x3=4y_1+x_2+x_3=4, which has (62)=15\binom{6}{2}=15 solutions. The same holds for x2x_2 and x3x_3, giving 3⋅15=453\cdot 15=45.

Two variables cannot both be ≥6\ge 6, because 6+6=12>106+6=12>10. So nothing was removed twice, and by inclusion–exclusion:

66−45=21.66-45=21.

Answer: 21

Problem 11

Hard

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 abcabc with a≥1a\ge 1 and a,b,c≤9a,b,c\le 9. Put a′=a−1≥0a'=a-1\ge 0:

a′+b+c=9.a'+b+c=9.

Without upper limits there are (112)=55\binom{11}{2}=55 solutions. Since the sum is 9, bb and cc can never exceed 9. But a′a' must stay at most 8 (because a≤9a\le 9), and a′=9a'=9 happens in exactly one solution: a′=9,b=c=0a'=9, b=c=0, which would mean a=10a=10. Removing it:

55−1=54.55-1=54.

Answer: 54

Problem 12

Hard

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: y1+y2+y3+y4=7y_1+y_2+y_3+y_4=7, yi≥0y_i\ge 0, where yi=xi−2y_i=x_i-2.

The condition x1≤5x_1\le 5 becomes y1≤3y_1\le 3.

All solutions: (103)=120\binom{10}{3}=120.

Bad solutions (y1≥4y_1\ge 4): set z1=y1−4z_1=y_1-4, so z1+y2+y3+y4=3z_1+y_2+y_3+y_4=3, which has (63)=20\binom{6}{3}=20 solutions.

120−20=100.120-20=100.

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: 4104^{10} 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 (n+k−1k−1)\binom{n+k-1}{k-1} or (n+k−1n)\binom{n+k-1}{n}?

Both. Choosing which k−1k-1 of the n+k−1n+k-1 positions hold bars is the same as choosing which nn hold stars, and (mr)=(mm−r)\binom{m}{r}=\binom{m}{m-r}.

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.