Discrete Mathematics and Combinatorics

Stars and Bars, Counting Ways to Share Identical Objects

Turn sharing identical items among people into arranging symbols in a row, then handle minimums, limits and inequalities.

Key takeaways

  • With zeros allowed, x₁ + x₂ + ... + xₖ = n has C(n + k − 1, k − 1) solutions in nonnegative integers.
  • If every part must be at least 1, the count is C(n − 1, k − 1), valid when n ≥ k ≥ 1.
  • Minimum requirements are handled by handing out the required objects first.
  • Maximum limits need a correction step, usually inclusion-exclusion, and the objects must be identical throughout.

There are C(n + k − 1, k − 1) ways to share n identical objects among k distinct recipients when some recipients may get nothing. The stars and bars method shows why: draw each object as a star, draw k − 1 dividing bars, and every arrangement of those symbols in a row is exactly one way of sharing.

C(a, b) means "a choose b", the number of ways to pick b positions out of a, and it equals a! / (b! (a − b)!).

The picture

Share 7 identical candies among 3 children, Ana, Ben and Cal. Write the candies as stars and use two bars to separate the three shares:

★★|★★★★|★        Ana 2, Ben 4, Cal 1
|★★★★★★★|        Ana 0, Ben 7, Cal 0
★★★||★★★★        Ana 3, Ben 0, Cal 4

Every sharing gives one such string, and every string of 7 stars and 2 bars gives one sharing. A string has 9 positions, and it is settled once you choose which 2 hold bars. So the number of sharings is C(9, 2) = 36.

This one-for-one matching between two collections is called a bijection. It lets you count the harder collection by counting the easier one.

The general formula

For n identical objects and k distinct recipients you need n stars and k − 1 bars, which is n + k − 1 symbols.

number of sharings = C(n + k − 1, k − 1) = C(n + k − 1, n)

10 candies, 4 children:  C(13, 3) = 286

In algebraic dress, this counts the solutions of x₁ + x₂ + x₃ + x₄ = 10 where each variable is a whole number that is 0 or more. Order of the variables matters: (10, 0, 0, 0) and (0, 10, 0, 0) are different solutions.

Stars and bars in four moves
1 Name the parts k distinct boxes, n identical objects 2 Meet minimums Hand out required objects first 3 Draw the row Remaining stars and k − 1 bars 4 Choose bar spots Count with a binomial coefficient Name the parts k distinct boxes, n identical objects 1 Meet minimums Hand out required objects first 2 Draw the row Remaining stars and k − 1 bars 3 Choose bar spots Count with a binomial coefficient 4

Why bars and not stars

You may wonder why there are k − 1 bars for k recipients. A single bar cuts a row into two pieces, two bars cut it into three, and in general k − 1 cuts make k pieces. The first piece goes to the first recipient, the second to the next, and so on.

Pieces may be empty. Two bars side by side give the middle recipient nothing, and a bar at the very start leaves the first recipient with nothing. That is why zeros need no special treatment.

You can also choose positions for the stars: C(n + k − 1, n) equals C(n + k − 1, k − 1), because picking the star positions settles the bar positions. Use whichever is quicker to compute.

A small case is worth listing in full. With 2 candies and 3 children the formula gives C(4, 2) = 6, and the six sharings are (2,0,0), (0,2,0), (0,0,2), (1,1,0), (1,0,1) and (0,1,1).

Everyone gets at least one

Now require each of the 4 children to receive at least one of the 10 candies. Give one to each child first. That leaves 6 candies to share freely among 4, so the count is C(9, 3) = 84.

There is a second way to see it. Line up the 10 stars. Bars must go in the gaps between neighboring stars, with at most one bar per gap, or some child would receive nothing. There are 9 gaps and 3 bars, giving C(9, 3) again.

positive solutions of x₁ + ... + xₖ = n:  C(n − 1, k − 1)

An ordered list of positive integers with a given sum is called a composition. Adding over all possible k shows that a positive integer n has 2ⁿ⁻¹ compositions: each of the n − 1 gaps either gets a bar or does not.

Other minimum requirements

The same trick handles any lower bounds. How many solutions does x + y + z = 12 have with x ≥ 2, y ≥ 1 and z ≥ 0?

Set aside 2 for x and 1 for y. Write x = a + 2 and y = b + 1, with a and b nonnegative. The equation becomes a + b + z = 9.

C(9 + 2, 2) = C(11, 2) = 55

A lower bound can also be negative. If x, y and z must each be at least −1 and add to 4, add 1 to each variable. The new variables are nonnegative and add to 7, giving C(9, 2) = 36.

Upper limits

Maximums are different, because you cannot hand out a limit in advance. How many solutions of x + y + z = 10 have each variable between 0 and 5?

First count all nonnegative solutions: C(12, 2) = 66. Then remove the bad ones. If x ≥ 6, subtract 6 from it, leaving a sum of 4, which has C(6, 2) = 15 solutions. The same holds for y and for z. Two variables cannot both be 6 or more, since that would need at least 12.

66 − 3 × 15 = 21

When two or more limits can be broken at once, add back the double subtractions, following inclusion-exclusion.

As a check, replace each variable v by 5 − v. The new variables still lie between 0 and 5 and now add to 5. A sum of 5 cannot break the limit, so the count is C(7, 2) = 21.

A problem with several limits broken

A larger example shows the full correction. Count the solutions of x + y + z = 15 with each variable from 0 to 7.

Without limits there are C(17, 2) = 136 solutions. If a chosen variable is at least 8, subtract 8 from it, leaving a sum of 7 with C(9, 2) = 36 solutions. Two variables cannot both reach 8, since that needs 16.

136 − 3 × 36 = 28

Replacing each v by 7 − v gives a sum of 21 − 15 = 6, which cannot break the limit of 7. The count is C(8, 2) = 28, as before.

Now lower the limit to 5. Variables are at least 6 when broken. One broken variable leaves a sum of 9, with C(11, 2) = 55 solutions. Two broken variables leave 3, with C(5, 2) = 10. Three would need 18. The total is 136 − 3 × 55 + 3 × 10 = 1, and indeed only (5, 5, 5) works.

Inequalities and a slack variable

To count solutions of x + y + z ≤ 10 in nonnegative integers, invent a fourth variable s for the unused amount. Then x + y + z + s = 10, and the count is C(13, 3) = 286.

This extra variable is often called a slack variable. It turns "at most" into "exactly", and it works because each solution of the inequality fixes s in only one way.

Hidden versions of the same problem

Many questions are stars and bars in disguise. Each one asks how many times each type appears.

Question Boxes Stars Count
Boxes of 12 donuts from 5 flavors 5 12 C(16, 4) = 1,820
Unordered rolls of 3 matching dice 6 3 C(8, 5) = 56
Terms of degree 3 in x, y, z 3 3 C(5, 2) = 10

A selection with repeats allowed and order ignored is called a multiset. The number of multisets of size r drawn from m types is C(r + m − 1, r), the fourth case in the permutation and combination table.

When the method does not apply

The objects must be identical. If the 10 candies were all different, each candy would independently choose one of 4 children, giving 4¹⁰ = 1,048,576 ways, by the multiplication rule.

The recipients must be distinct. If you are splitting 10 candies into unlabeled piles, (7, 2, 1) and (1, 2, 7) are the same. You are then counting integer partitions, which have no comparably simple formula.

Be careful with probability too. Roll 3 dice, and the 56 unordered results are not equally likely. Three sixes can happen in only 1 of the 216 ordered outcomes, while the result {1, 2, 3} happens in 6. For probabilities with fair dice, count ordered outcomes.

Paths on a grid in disguise

A string of stars and bars can also be read as a walk. Treat each star as a step to the east and each bar as a step to the north. A string with n stars and k − 1 bars is then a route across a rectangle n blocks wide and k − 1 blocks tall that only moves east or north.

The share of the first recipient is the number of eastward steps before the first northward one, and so on. This is why the same binomial coefficient counts both problems, as described in counting lattice paths.

Sequences that never decrease

How many 4-digit strings have digits that never decrease from left to right, such as 0337 or 5555? Once you know which digits appear and how often, there is only one way to write them in order. So you are choosing a multiset of size 4 from 10 digit types:

C(4 + 10 − 1, 4) = C(13, 4) = 715

If the digits must strictly increase, no repeats are possible, and the count is the plain combination C(10, 4) = 210.

The same reasoning counts the nondecreasing results of repeated measurements, or the ways to fill a sorted list from a fixed set of values. Whenever order is forced, only the tally of each type remains free.

Practice problems

  1. Share 8 identical books among 3 shelves, empty shelves allowed. You should get C(10, 2) = 45.
  2. Repeat with no empty shelf. The answer is C(7, 2) = 21.
  3. Count nonnegative solutions of a + b + c + d = 6 with a ≥ 2. Set aside 2, then compute C(7, 3) = 35.
  4. Count solutions of x + y = 8 with both variables at most 5. List them to confirm that there are 3.

If you want to push further, the same counts appear as coefficients of 1/(1 − x)ᵏ, a connection explored with generating functions. That viewpoint handles awkward limits by multiplying simple series together.

A short checklist

Before using the formula, run through these questions. Are the objects interchangeable? Are the recipients distinct and fixed in number? Must every object be handed out, or may some be left over? Does any recipient have a minimum or a maximum?

If the first two answers are yes, the method applies. Leftovers call for a slack variable, minimums are paid in advance, and maximums are corrected afterward. State the values of n and k in words, such as "10 identical candies, 4 named children", before substituting. Swapping the two is the most common error, and C(13, 3) for the candies becomes the quite different C(13, 9) if you do.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic