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.
This approach involves exploring all possible distributions of candies among the children using backtracking. We try distributing candies to each child recursively and backtrack when an invalid distribution is reached. This ensures that we count all valid ways to distribute candies.
The C solution recursively distributes candies among the children while respecting the condition that no child receives more than the given limit. Each recursive call attempts to distribute a certain number of candies to the current child and then proceeds to the next.
Time Complexity: O(limit^3) since there are three children and each child can take up to 'limit' candies.
Space Complexity: O(1) if we ignore recursion stack space.
This approach uses a nested loop to iterate over all possible combinations of candy distributions to the three children. By explicitly looping through possibilities in a structured way, we count valid combinations that respect each child's candy limit.
The C implementation uses a triple nested loop to count all valid combinations. Each loop assigns candies to one child, checking that the remaining amount for the third child does not exceed the limit.
Time Complexity: O(n × limit^2) as it iterates over children with nested loops.
Space Complexity: O(1) since we simply count configurations.
According to the problem description, we need to distribute n candies to 3 children, with each child receiving between [0, limit] candies.
This is equivalent to placing n balls into 3 boxes. Since the boxes can be empty, we can add 3 virtual balls, and then use the method of inserting partitions, i.e., there are a total of n + 3 balls, and we insert 2 partitions among the n + 3 - 1 positions, thus dividing the actual n balls into 3 groups, and allowing the boxes to be empty. Therefore, the initial number of schemes is C_{n + 2}^2.
We need to exclude the schemes where the number of balls in a box exceeds limit. Consider that there is a box where the number of balls exceeds limit, then the remaining balls (including virtual balls) have at most n + 3 - (limit + 1) = n - limit + 2, and the number of positions is n - limit + 1, so the number of schemes is C_{n - limit + 1}^2. Since there are 3 boxes, the number of such schemes is 3 times C_{n - limit + 1}^2. In this way, we will exclude too many schemes where the number of balls in two boxes exceeds limit at the same time, so we need to add the number of such schemes, i.e., 3 times C_{n - 2 times limit}^2.
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Backtracking Approach | Time Complexity: O(limit^3) since there are three children and each child can take up to 'limit' candies. |
| Iterative Combinatorial Approach | Time Complexity: O(n × limit^2) as it iterates over children with nested loops. |
| Combinatorial Mathematics + Principle of Inclusion-Exclusion | — |
| 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 |
2928. Distribute Candies Among Children I (Leetcode Easy) • Programming Live with Larry • 654 views views
Watch 7 more video solutions →Practice Distribute Candies Among Children I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor