Counting allocations of identical items into labeled boxes
Placing identical items into labeled boxes is counted with stars and separators. Five items in three boxes can be arranged in 21 ways.
Identical items, labeled boxes
Imagine five identical coins going into three boxes marked 1, 2 and 3. Because the coins are alike, only the number in each box matters. An arrangement is a list of three counts, one per box, that add up to five. The list (2, 0, 3) differs from (3, 0, 2) because the boxes have labels. Boxes may be empty, so counts of zero are allowed. The task is to count how many such lists exist. Writing them all out is possible for five coins, but the method below scales to any number of items and boxes with a single combination.
Stars and separators
Draw the five items as stars: *****. To split them among three boxes, insert two separators (bars) among the stars. Stars before the first bar go in box 1, stars between the bars go in box 2, and stars after the second bar go in box 3. The string **|*|** means 2, 1 and 2. The string |*****| puts nothing in box 1, all five in box 2 and nothing in box 3. Each arrangement of five stars and two bars is a different allocation, and each allocation has exactly one such string. That one-to-one match is what lets you count strings instead of allocations.
Counting the strings
A string has seven symbols, five stars and two bars. It is fixed once you choose the two positions that hold bars, so the count is C(7, 2) = 7! ÷ (2! × 5!) = 21. The combination calculator returns 21 for n = 7 and r = 2. Choosing the five star positions instead gives C(7, 5) = 21, the same total, because choosing the bars and choosing the stars split the seven positions in the same way. Both worked cases confirm the figure. In general, n items in k boxes give C(n + k - 1, k - 1) allocations, where n + k - 1 is the string length.
Empty boxes and the ends
Empty boxes correspond to bars sitting next to each other or at an end of the string. Two adjacent bars with no star between them mean the middle box is empty. A bar at the very start means box 1 is empty, and a bar at the end means box 3 is empty. These cases come for free in the stars-and-bars count, which is why it counts allocations with zeros allowed. If a problem requires every box to have at least one item, place one item in each box first. That leaves 2 items to place in 3 boxes, which is C(4, 2) = 6 allocations.
Further examples
Six identical items in four labeled boxes need three bars among six stars, a string of 9 symbols, so the count is C(9, 3) = 84. Ten items in three boxes give C(12, 2) = 66. Four items in two boxes give C(5, 1) = 5, which matches the list (0, 4), (1, 3), (2, 2), (3, 1) and (4, 0). The last one is easy to check by hand. For each case, the string length is the number of items plus the number of boxes minus one, and the number to choose is the number of bars, which is the number of boxes minus one.
Labeled versus unlabeled boxes
The count of 21 treats (2, 0, 3) and (3, 0, 2) as different. If the boxes were unlabeled, those two would be the same arrangement, and the problem becomes one of partitions of the number 5 into at most three parts. For five items there are 5 such partitions: 5, 4 + 1, 3 + 2, 3 + 1 + 1 and 2 + 2 + 1. That is far fewer than 21, and there is no simple combination formula for it. Listing is the practical way to count partitions of small numbers. The word labeled in the problem statement is what justifies the combination count, so check that detail before using the method.
Distinct items are a different problem
If the five items are different, each can go in any of 3 boxes independently, which gives 3^5 = 243 allocations. That is much larger than 21, because distinguishable items multiply the possibilities. The stars-and-bars method needs the items to be identical. The test is whether swapping two items between boxes would produce a visibly different outcome. With identical coins it would not. With a penny and a dime it would. Matching the model to the situation is the main skill in these problems, and the arithmetic is the easy part.
Solutions of an equation
Stars and bars is the same count as the number of nonnegative integer solutions to x₁ + x₂ + x₃ = 5. Each solution is one allocation: x₁ is the count in box 1, x₂ in box 2 and x₃ in box 3. The triple (2, 0, 3) is one solution and (3, 0, 2) is another. If each variable must be at least 1, substitute yᵢ = xᵢ - 1, which turns the equation into y₁ + y₂ + y₃ = 2 with nonnegative variables, and the count becomes C(4, 2) = 6. If one variable has a different lower bound, such as x₁ ≥ 2, subtract 2 from the total first. Setting the problem up as an equation and then adjusting the total is a reliable way to handle minimum amounts. The hockey-stick relation also appears here: summing the allocations for every item count from 0 to 5 in three boxes gives the allocations of 5 items in four boxes, C(8, 3) = 56.
Where it applies and where it stops
The pattern appears when distributing identical units, such as tickets, tokens or equal portions, among labeled recipients, and when counting the nonnegative whole-number solutions of an equation like x + y + z = 5. It does not include limits on how much a box can hold, minimum amounts above zero, or cost. The combination calculator evaluates the count exactly for small numbers. For larger inputs the values grow quickly, and you may reach the display limits of the tool. The count tells you how many allocations exist, and nothing about which one is best. It also assumes that every allocation is equally allowed. If a box has a capacity, for instance at most 3 items, the arrangements that exceed it must be removed, and that needs a separate count or an inclusion-exclusion step. The default case of five items in three boxes has no such limit, so all 21 lists count, including (5, 0, 0) with all five items in one box.
Reproduce the checks
Separator positions
Open the Combination calculator and enter:
- Total items n
7- Items selected r
2
Expected output:
Combinations
21Five stars and two separators encode every allocation once.
Equivalent star-position count
Open the Combination calculator and enter:
- Total items n
7- Items selected r
5
Expected output:
Combinations
21Choosing the complementary positions gives the same strings.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.