Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
5 2
3 3
count-ways-to-distribute-candies)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2928: Distribute Candies Among Children I
class Solution {
public int distributeCandies(int n, int limit) {
if (n > 3 * limit) {
return 0;
}
long ans = comb2(n + 2);
if (n > limit) {
ans -= 3 * comb2(n - limit + 1);
}
if (n - 2 >= 2 * limit) {
ans += 3 * comb2(n - 2 * limit);
}
return (int) ans;
}
private long comb2(int n) {
return 1L * n * (n - 1) / 2;
}
}
// Accepted solution for LeetCode #2928: Distribute Candies Among Children I
func distributeCandies(n int, limit int) int {
comb2 := func(n int) int {
return n * (n - 1) / 2
}
if n > 3*limit {
return 0
}
ans := comb2(n + 2)
if n > limit {
ans -= 3 * comb2(n-limit+1)
}
if n-2 >= 2*limit {
ans += 3 * comb2(n-2*limit)
}
return ans
}
# Accepted solution for LeetCode #2928: Distribute Candies Among Children I
class Solution:
def distributeCandies(self, n: int, limit: int) -> int:
if n > 3 * limit:
return 0
ans = comb(n + 2, 2)
if n > limit:
ans -= 3 * comb(n - limit + 1, 2)
if n - 2 >= 2 * limit:
ans += 3 * comb(n - 2 * limit, 2)
return ans
// Accepted solution for LeetCode #2928: Distribute Candies Among Children I
fn distribute_candies(n: i32, limit: i32) -> i32 {
fn f(n: i32, people: i32, limit: i32) -> i32 {
if people >= 3 {
if n == 0 {
return 1;
}
return 0;
}
let mut ret = 0;
let limit = std::cmp::min(n, limit);
for i in 0..=limit {
ret += f(n - i, people + 1, limit);
}
ret
}
f(n, 0, limit)
}
fn main() {
let ret = distribute_candies(3, 3);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(distribute_candies(5, 2), 3);
assert_eq!(distribute_candies(3, 3), 10);
}
// Accepted solution for LeetCode #2928: Distribute Candies Among Children I
function distributeCandies(n: number, limit: number): number {
const comb2 = (n: number) => (n * (n - 1)) / 2;
if (n > 3 * limit) {
return 0;
}
let ans = comb2(n + 2);
if (n > limit) {
ans -= 3 * comb2(n - limit + 1);
}
if (n - 2 >= 2 * limit) {
ans += 3 * comb2(n - 2 * limit);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.