iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
For LeetCode 2929, count ordered triples (a, b, c) of nonnegative candy amounts that sum to n, with each amount at most limit. Inclusion-exclusion applied to the stars-and-bars count gives C(n+2, 2) − 3C(n−limit+1, 2) + 3C(n−2limit, 2) − C(n−3limit−1, 2). Treat each binomial term as zero when its upper value is less than 2. That convention handles cases where there are not enough candies for an over-limit allocation.
What the formula counts
LeetCode 2929 asks for the number of ways to distribute n candies among three children when no child may receive more than limit. The children are distinguishable, so giving two different amounts to two named children counts as a different allocation. In symbols, count the triples satisfying:
a + b + c = n, where 0 ≤ a, b, c ≤ limit.
The statement gives the constraints 1 ≤ n ≤ 106 and 1 ≤ limit ≤ 106. See the LeetCode problem statement.
Start with the unrestricted count
First, ignore the maximum. The number of nonnegative integer triples with sum n is C(n+2, 2), by stars and bars. Imagine placing two dividers among a row of n candies: the dividers split the candies into three groups, and a group may be empty.
#1 Best Overall
- Careercup, Easy To Read
- Condition : Good
- Compact for travelling
This counts every allocation with no upper bound, including allocations where one or more children receive too many candies. The remaining terms remove those invalid allocations.
Apply inclusion-exclusion to the cap violations
Call an allocation a violation for a particular child if that child receives at least limit + 1 candies. Inclusion-exclusion subtracts single-child violations, restores allocations counted twice because two children violate the cap, then subtracts the triple overlap.
Subtract allocations where one child exceeds the limit
Choose a particular child and reserve limit + 1 candies for them. There are then n − limit − 1 candies left to distribute without restrictions among the three children. Stars and bars counts those distributions as:
C((n − limit − 1) + 2, 2) = C(n − limit + 1, 2).
Rank #2
Any of the three children could be the one over the limit, so subtract 3C(n − limit + 1, 2).
Restore allocations where two children exceed the limit
When two specified children each receive at least limit + 1, reserving that amount for both leaves n − 2(limit + 1) candies. The unrestricted count is:
C((n − 2(limit + 1)) + 2, 2) = C(n − 2limit, 2).
Recommended Free Tools
There are three pairs of children. Add 3C(n − 2limit, 2) to restore these allocations, which were removed once for each violating child.
Subtract the triple overlap
If all three children exceed the limit, reserve limit + 1 candies for each. The remainder is n − 3(limit + 1), whose unrestricted count is:
C((n − 3(limit + 1)) + 2, 2) = C(n − 3limit − 1, 2).
Subtract this once. Combining the four inclusion-exclusion terms produces the formula:
answer = C(n+2, 2) − 3C(n−limit+1, 2) + 3C(n−2limit, 2) − C(n−3limit−1, 2)
For this expression, define C(x, 2) = x(x − 1)/2 when x ≥ 2, and C(x, 2) = 0 when x < 2. A shifted term may have fewer than two available units—or a negative shifted value—so it contributes no distributions.
Check the formula against LeetCode’s examples
Example: n = 5, limit = 2
The calculation is C(7, 2) − 3C(4, 2) + 3C(1, 2) − C(−2, 2) = 21 − 18 + 0 − 0 = 3. The valid allocations are the three permutations of (1, 2, 2).
Example: n = 3, limit = 3
No child can exceed the cap, so the answer is the unrestricted count: C(5, 2) = 10. Both results match the examples in the official problem statement.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Alternative derivation: sum over the first child’s amount
If you prefer to count feasible choices directly, fix the first child’s allocation as i. It must be large enough that the other two can hold the remaining candies, and no larger than the cap or the total:
Best Value
max(0, n − 2·limit) ≤ i ≤ min(n, limit).
For each such i, let the second child receive j. The third child’s amount is then determined by the total, and the valid range for j is:
max(0, n − i − limit) ≤ j ≤ min(limit, n − i).
Every integer in that inclusive interval yields a valid allocation. Therefore the number of choices for this fixed i is the upper endpoint minus the lower endpoint plus one. Summing those counts over all feasible i gives the same answer. This direct method makes the valid ranges explicit; the inclusion-exclusion formula avoids iterating over them and evaluates in constant time. The feasible ranges are also described in the CodeJeet explanation; the stars-and-bars and inclusion-exclusion approach is outlined by LeetCode.ca.
Quick Recap
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.

