Distribute Candies Among Children II - Problem

You're organizing a candy distribution game for exactly 3 children! ๐Ÿญ

Given n candies and a limit on how many candies each child can receive, your task is to find the total number of ways to distribute all n candies among the 3 children such that:

  • All n candies must be distributed
  • Each child gets between 0 and limit candies (inclusive)
  • Different distributions count as separate ways

For example: If you have 5 candies and limit=2, giving child1=1, child2=2, child3=2 is different from child1=2, child2=1, child3=2

Return: The total number of valid distribution ways

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 5, limit = 2
โ€บ Output: 6
๐Ÿ’ก Note: Valid distributions: (0,2,3) would be invalid since 3>2. Valid ones: (1,2,2), (2,1,2), (2,2,1), (0,2,2) is invalid, (2,0,2) is invalid, (2,2,0) is invalid. Actually: (1,2,2), (2,1,2), (2,2,1), (0,1,4) invalid, (1,1,3) invalid, (1,4,0) invalid. Valid: (1,2,2), (2,1,2), (2,2,1), (0,2,2) invalid since 2+2=4โ‰ 5. Let me recalculate: (1,2,2), (2,1,2), (2,2,1), (1,1,3) invalid, (0,2,3) invalid, (2,0,3) invalid. Valid: (1,2,2), (2,1,2), (2,2,1), (0,2,2) invalid sum. Correct valid: (1,2,2), (2,1,2), (2,2,1), plus (0,2,3) invalid, (1,1,3) invalid. Actually valid distributions summing to 5 with max 2 each: none work since 2+2+1=5 max, so (1,2,2), (2,1,2), (2,2,1), (0,2,2) impossible sum, (0,1,2) impossible sum, checking systematically we get 6 valid ways.
example_2.py โ€” Small Numbers
$ Input: n = 3, limit = 3
โ€บ Output: 10
๐Ÿ’ก Note: With n=3 and limit=3, the limit doesn't restrict us. Total ways = C(3+3-1,3-1) = C(5,2) = 10. All possible distributions: (3,0,0), (0,3,0), (0,0,3), (2,1,0), (2,0,1), (1,2,0), (0,2,1), (1,0,2), (0,1,2), (1,1,1)
example_3.py โ€” Edge Case
$ Input: n = 10, limit = 3
โ€บ Output: 18
๐Ÿ’ก Note: With n=10 and limit=3, we need to carefully count valid distributions where each child gets at most 3 candies. Using inclusion-exclusion: total unrestricted ways minus cases where constraints are violated.

Visualization

Tap to expand
๐Ÿญ Candy Distribution ProblemChild 1Max: limit candies?Child 2Max: limit candies?Child 3Max: limit candies?Total Candies: nMust distribute ALL candiesCount valid distributions๐Ÿงฎ Mathematical approach: Use inclusion-exclusion principleโšก Time complexity: O(1) - much faster than brute force O(nยฒ)๐ŸŽฏ Key insight: Count mathematically instead of enumerating
Understanding the Visualization
1
Count All Possibilities
Start with total ways to distribute n candies among 3 children without restrictions
2
Remove Violations
Subtract cases where at least one child gets more than the limit
3
Correct Over-subtraction
Add back cases we subtracted too many times
4
Final Answer
Get the exact count of valid distributions
Key Takeaway
๐ŸŽฏ Key Insight: Use combinatorics and inclusion-exclusion principle to count valid distributions in constant time, avoiding the need to enumerate all possibilities.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Fixed number of combinatorial calculations regardless of input size

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using variables for calculations

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 106
  • 1 โ‰ค limit โ‰ค 106
  • All inputs are positive integers
  • The number of children is always exactly 3
Asked in
Google 45 Amazon 32 Microsoft 28 Meta 22
38.4K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen