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:
The 3 ways are: (0,2,3) - invalid since 3>2, (1,1,3) - invalid, (1,2,2), (2,1,2), (2,2,1). Only 3 valid distributions: (1,2,2), (2,1,2), (2,2,1).
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
Understanding the Visualization
1
Input
n=5 candies, limit=2 per child
2
Process
Find all valid distributions (x,y,z) where x+y+z=5 and x,y,z≤2
3
Output
Count = 3 valid ways
Key Takeaway
🎯 Key Insight: Use combinatorics to count distributions efficiently instead of enumeration
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code