Use stars and bars to count nonnegative integer solutions directly, and to handle minimum requirements after subtracting them from the total. For upper bounds, start with that unrestricted count and use inclusion–exclusion to remove solutions that exceed one or more caps. In bounded distribution problems, these methods work together rather than compete.
What stars and bars counts
For the equation x1 + x2 + … + xk = n, where every variable is a nonnegative integer, the number of solutions is C(n + k − 1, k − 1). Imagine n stars divided among k variables by k − 1 bars. Choosing the bar positions determines each variable’s share; an empty segment represents zero. This is the standard stars-and-bars bijection. See the stars-and-bars explanation.
As an Amazon Associate I earn from qualifying purchases.
How to handle minimum requirements
If each variable must meet a minimum ai, assign those minimum amounts first. Set yi = xi − ai; the new variables are nonnegative and sum to n − Σai. When that residual total is nonnegative, the count is C(n − Σai + k − 1, k − 1). If it is negative, there are no solutions. The lower-bound shift and stars-and-bars count are described in the University of Wisconsin–Madison combinatorics notes.
Recommended Free Tools
Why upper bounds need a different step
A cap such as xi ≤ bi does not simply shift the total: it rules out solutions in which that variable is too large. Let Ai be the set of unrestricted solutions where xi > bi. The valid solutions are those outside every Ai. Subtracting each single-violation set is not enough, because a solution violating two caps would then be subtracted twice. Inclusion–exclusion adds back pairwise overlaps, subtracts triple overlaps, and continues with alternating signs. The bounded nonnegative-solution setup is laid out in the University of Illinois lecture notes.
#1 Best Overall
Count bounded solutions with inclusion–exclusion
-
Count all nonnegative solutions without caps: C(n + k − 1, k − 1).
-
For each subset J of capped variables, count solutions that exceed every cap in that subset. For each i in J, set yi = xi − (bi + 1). This shift makes the violating variables nonnegative and reduces the total by Σi∈J(bi + 1).
-
Apply stars and bars to the remaining total and combine the subset counts with alternating signs. If the shifted total is negative, that intersection contributes zero.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
The general count for nonnegative integer solutions subject to upper bounds is:
ΣJ (−1)|J| C(n − Σi∈J(bi + 1) + k − 1, k − 1)
Here J ranges over every subset of the capped variables, including the empty subset. Treat a term as zero when its shifted total is negative. For the empty subset, the inner sum is zero, giving the unrestricted baseline. Each nonempty subset represents an intersection of violations, shifted so it can be counted with stars and bars.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Choosing a method for your problem
| Constraint or situation | Useful setup | What to watch |
|---|---|---|
| Nonnegative variables, no upper caps | Stars and bars directly | Zero is allowed; the variables must be integers. |
| Minimum for each variable | Subtract the minimums, then use stars and bars | Check that the residual total is nonnegative. |
| Upper caps on a few variables | Inclusion–exclusion, with stars and bars for each intersection | Track the alternating signs and include overlaps. |
| Finite allowed range for each variable | Generating function: extract the coefficient of zn in ∏i(1 + z + … + zbi) | Each factor represents one variable’s allowed values. This is a compact expression, not a claim that it is always faster to compute. |
The coefficient method encodes each variable’s allowed values in its own factor; multiplying the factors combines choices whose values sum to n. Inclusion–exclusion makes the correction terms explicit, which can be easier to inspect for a few caps. The number of caps determines how many intersections appear, so the alternating sum can grow lengthy. The Applied Combinatorics textbook covers both inclusion–exclusion and generating functions.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




