Watch 8 video solutions for Distribute Candies Among Children I, a easy level problem involving Math, Combinatorics, Enumeration. This walkthrough by Programming Live with Larry has 654 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 <= 501 <= limit <= 50Problem Overview: You have n identical candies and 3 children. Each child can receive at most limit candies. Count how many valid distributions exist where the total number of candies given equals n.
Approach 1: Backtracking Enumeration (Time: O(n^2), Space: O(1) auxiliary)
This method explicitly enumerates possible distributions. Assign candies to the first child from 0 to limit, then assign candies to the second child within the remaining budget, and compute what the third child must receive. If the third child's value is within [0, limit], the distribution is valid. Conceptually this is a backtracking or exhaustive enumeration approach where you try combinations and prune invalid states when the remaining candies become negative or exceed the limit.
The key idea is simple iteration over feasible assignments while ensuring the total sum equals n. This approach is easy to implement and mirrors the thinking process of manually distributing candies. It falls under enumeration and basic math reasoning.
Approach 2: Iterative Combinatorial Counting (Time: O(n), Space: O(1))
Instead of exploring every triple explicitly, iterate the number of candies given to the first child. Suppose the first child receives x. The remaining candies are n - x, which must be split between the other two children. For the second child, valid values must satisfy both the remaining sum and the limit constraint. That creates a bounded range max(0, n - x - limit) to min(limit, n - x). The number of valid assignments for the second child equals the size of this interval, and the third child's candies are automatically determined.
This converts the problem into counting valid integer solutions rather than generating them. The insight comes from combinatorics: once the first child is fixed, the remaining distribution becomes a constrained two-variable equation. Iterating x from 0 to limit and summing the valid counts gives the final answer efficiently.
Recommended for interviews: The combinatorial counting approach is preferred. It demonstrates that you can convert a brute-force enumeration into a mathematical counting problem and reduce complexity to O(n) with constant space. Starting with the brute-force reasoning is still useful because it shows understanding of the constraints before optimizing.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking Enumeration | O(n^2) | O(1) | When exploring all valid assignments explicitly or explaining brute-force reasoning in interviews |
| Iterative Combinatorial Counting | O(n) | O(1) | Best general solution when counting valid distributions efficiently |