For LeetCode 2929, count ordered assignments of n candies to three children, allowing zero candies but capping each child at limit. The most useful edge checks are the total-capacity boundary, the unrestricted range, and the inclusion-exclusion terms that become zero when their arguments are negative.
What counts as a valid distribution?
The three children are distinct, so assignments are ordered: giving two candies to one child and one to another is different from swapping those recipients. A child may receive zero. The official statement gives 1 ≤ n ≤ 106 and 1 ≤ limit ≤ 106, with examples n = 5, limit = 2 yielding 3 and n = 3, limit = 3 yielding 10. See the official LeetCode 2929 statement.
As an Amazon Associate I earn from qualifying purchases.
Which boundary cases should you check?
n > 3 × limit: Return 0. The three children together cannot hold enough candies.n = 3 × limit: Return 1. Only the allocation(limit, limit, limit)reaches the total.n ≤ limit: The cap cannot exclude any allocation. The answer is the unrestricted count,(n + 2)(n + 1) / 2.n = 1: There are 3 allocations, since exactly one of the three children receives the candy andlimit ≥ 1.
How does the direct counting method work?
Choose the first child’s amount, then count the feasible amounts for the second child; the third child’s amount is determined by the remainder. This directly expresses the constraints and can help when verifying a formula.
- Let the first child receive
i. Its feasible range ismax(0, n − 2 × limit)throughmin(n, limit), inclusive. - For each
i, let the second child’s amount range frommax(0, n − i − limit)throughmin(limit, n − i), inclusive. - Add the number of integers in that second range:
upper − lower + 1. The third child receivesn − i − second, which is within the cap by construction.
This takes linear time in the feasible range, at most about one million iterations for the stated constraints, and constant extra space.
#1 Best Overall
- CLASSIC BEGINNER GAME: Do you remember playing Candy Land when you were a kid. Introduce new generations to this sweet kids' board game
- RACE TO THE CASTLE: Players encounter all kinds of "delicious" surprises as they move their cute gingerbread man pawn around the path in a race to the castle
- NO READING REQUIRED TO PLAY: For kids ages 3 and up, Candy Land can be a great game for kids who haven't learned how to read yet
- GREAT GAME FOR LITTLE ONES: The Candy Land board game features colored cards, sweet destinations, and fun illustrations that kids love
How does the constant-time inclusion-exclusion formula work?
Define W(x) as the number of ways to distribute x candies among three labeled children with no upper caps:
W(x) = 0whenx < 0.W(x) = (x + 2)(x + 1) / 2whenx ≥ 0.
Then set k = limit + 1 and calculate:
answer = W(n) − 3 × W(n − k) + 3 × W(n − 2k) − W(n − 3k)
Rank #2
- HERSHEY BINGO GAME - This Hershey Bingo Game is the perfect way for kids to enjoy Hershey’s chocolate and candy without all the sugar!
- OFFICIALLY LICENSED HERSHEY PRODUCT - This game is officially licensed by Hershey.
- PERFECT FOR KIDS 7 AND UP - The bright colors and easy to understand rules make this game perfect for kids. Suitable for kids 7 years and up.
- PERFECT FOR PARTIES - Whether you're hosting a birthday party, family gathering, or just want some fun with friends, this game is here to please. With 6 game boards, 1 spinner, and 84 custom tokens, there's plenty of fun to be had.
- MASTERPIECES - An American puzzle and game company committed to creating quality products.
The shift is limit + 1 because a child first exceeds the cap by receiving that many candies. Subtract allocations where one selected child has this excess; add back allocations where two selected children do; subtract the cases where all three do. Any term with a negative argument contributes zero. The formula takes constant time and constant space.
Free tools Windows power users keep installed
One-click scans. No signup required.
What implementation mistakes cause edge-case failures?
- Using
limitinstead oflimit + 1in the shifts: This removes valid allocations in which a child receives exactlylimit. - Keeping negative formula arguments: Define
W(x)to return zero for negativex; do not evaluate the triangular expression for them. - Forgetting the ordering: Count distributions by recipient, not merely the different candy amounts that appear in a triple.
- Assuming every child must receive candy: Zero is permitted by the statement.
- Using 32-bit arithmetic: At
n = 106, the unrestricted count is(106 + 2)(106 + 1) / 2, about5 × 1011. Use an integer type wide enough for intermediate products and the result.
Which method should you use?
Use the direct sum when you want each constraint to be visible and easy to trace. Use inclusion-exclusion when you want constant runtime; its main implementation risks are the limit + 1 shift and negative arguments. Both methods count labeled, ordered allocations and require constant extra space.
Quick Recap
Best Value
- THE CLASSIC: Everyone knows the game of Candy Land. This classic edition features the original, charming artwork from the games' early days
- RACE TO HOME SWEET HOME: Kids encounter all kinds of delicious fun as they move their cute gingerbread pawn through Gum Drop Mountains, Lollypop Woods, the Molasses Swamp and more
- NO READING REQUIRED: Since it's for kids ages 3 and up, Candy Land is suitable for those who haven't learned how to read yet
- A FAMILY FAVORITE: Cherish your child's early years by making memories with Candy Land. It's called a Rite of Passage game because it has been enjoyed generation after generation
- SKILLS: All in the name of fun. Children will learn counting, turn taking, color recognition, and color matching
Rank #4
- SWEET ADVENTURE AWAITS: Join the race through the Candy Factory to collect Chocolate, Gummies, and other candy before the mischievous trolls take over! Fast-paced, laugh-filled family board game for kids ages 4-6 keeps youngsters engaged from start to finish.
- SUPER EASY-TO-LEARN GAMEPLAY: Roll the die, move your character, and complete hilarious challenges to outsmart the trolls. This game for kids ages 4-8 has quick setup and simple rules, so even younger players can play this social skills board game right away.
- BUILDS SKILLS THROUGH PLAY: Encourages teamwork, critical thinking, and creativity as players solve challenges and make strategic choices. Perfect educational board game for kids ages 4-6 for developing social and problem-solving skills while having a blast.
- EDUCATIVE GAME WITH QUICK ROUNDS: Ideal for family bonding game nights, 1st, 2nd grade classrooms, homeschool learning activity and kindergarten. Short 15-20-minute rounds make this learning game easy to fit in one sweet adventure—or several!
- PERFECT GIFT FOR KIDS AGES 4+: The Candy Factory preschool board game includes 1 colorful game board, 5 adorable character game pieces, 30 troll challenge cards, 50 candy tokens, and 1 die. This toddler board game is built to last with durable, child-safe materials!
Rank #3
- BRING BACK MEMORIES: Do you remember playing classic Candy Land board game when you were a kid? Introduce new generations to classic childrens games and this new Unicorn Edition one of the popular kids board games
- Fun game for little ones: the candy land unicorn edition board game lets kids imagine they're on an exciting adventure along a magical path to rainbow mountain
- No reading required to play: kids draw cards to guide their unicorn along the rainbow path the candy land game is a great game for kids who haven't learned how to read yet
- Bring back memories: do you remember playing classic candy land when you were a kid? introduce new generations to this unicorn edition of the popular kids' board game
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

