Distribute Candies Among Children II - Problem
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.
Each distribution must assign a non-negative number of candies to each child, and the sum of all candies distributed must equal n.
Input & Output
Example 1 — Basic Case
$
Input:
n = 5, limit = 2
›
Output:
3
💡 Note:
We need to find all (x,y,z) where x+y+z=5 and 0≤x,y,z≤2. The valid distributions are: (1,2,2), (2,1,2), (2,2,1). Total: 3 ways.
Example 2 — Small Input
$
Input:
n = 3, limit = 3
›
Output:
10
💡 Note:
All possible distributions are valid since limit=3 ≥ n=3. Total ways = C(3+2,2) = C(5,2) = 10.
Example 3 — Tight Constraint
$
Input:
n = 2, limit = 1
›
Output:
3
💡 Note:
Valid ways: (0,1,1), (1,0,1), (1,1,0). Each child gets at most 1 candy and total is 2.
Constraints
- 1 ≤ n ≤ 50
- 1 ≤ limit ≤ 50
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code