Watch the video solution for Distribute Candies Among Children III, a hard level problem involving Math, Combinatorics. This walkthrough by Programming Live with Larry has 363 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given two positive integers n and limit.
Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.
Example 1:
Input: n = 5, limit = 2 Output: 3 Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
Example 2:
Input: n = 3, limit = 3 Output: 10 Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
Constraints:
1 <= n <= 1081 <= limit <= 108Problem Overview: You are given n candies and three children. Each child can receive at most limit candies. The task is to count how many valid distributions exist such that the total candies distributed equals n while no child exceeds the limit.
This is a classic counting problem. Without restrictions, distributing n identical items across three variables (a + b + c = n) can be solved using the stars and bars formula. The challenge comes from the upper bound constraint (a, b, c ≤ limit). Handling that constraint efficiently leads to a combinatorics-based solution.
Approach 1: Enumerate Valid Pairs (Brute Force Iteration) (Time: O(limit^2), Space: O(1))
Iterate over the candies given to the first two children. For each pair (a, b), compute c = n - a - b. The distribution is valid if 0 ≤ a ≤ limit, 0 ≤ b ≤ limit, and 0 ≤ c ≤ limit. This brute force approach checks every feasible pair and increments a counter when the third child also satisfies the constraint. It works because the number of children is fixed at three, but the nested iteration makes it quadratic in the worst case.
This approach is straightforward and useful for validating logic during development. However, it becomes inefficient when limit is large.
Approach 2: Combinatorial Mathematics + Principle of Inclusion-Exclusion (Time: O(1), Space: O(1))
First ignore the upper bound. The number of ways to distribute n candies among three children is given by the stars and bars formula: C(n + 2, 2). This counts all non‑negative integer solutions to a + b + c = n.
Next remove distributions where at least one child exceeds the limit. If child a gets more than limit, substitute a' = a - (limit + 1). The equation becomes a' + b + c = n - (limit + 1). The number of such solutions is C(n - limit + 1, 2). Apply the same logic for each child.
However, subtracting these individually double‑counts cases where two children exceed the limit. The combinatorics technique called the principle of inclusion‑exclusion fixes this by adding back intersections of invalid sets. With only three variables, the formula remains small and can be computed with a few combination calculations.
This turns the entire counting process into constant time arithmetic. No loops are required, and the memory footprint stays constant. Problems involving bounded integer solutions often reduce to this exact pattern using mathematical counting.
Recommended for interviews: The combinatorial + inclusion‑exclusion solution is what interviewers expect. It shows that you recognize the equation a + b + c = n as a stars‑and‑bars problem and know how to enforce upper bounds mathematically. Mentioning the brute force iteration first demonstrates understanding, but deriving the O(1) combinatorial formula demonstrates stronger problem‑solving skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Enumerate Valid Pairs (Brute Force) | O(limit^2) | O(1) | Useful for understanding the constraints or validating logic for small limits |
| Combinatorial Mathematics + Inclusion‑Exclusion | O(1) | O(1) | Optimal approach for large n and limit; standard mathematical solution expected in interviews |