Problem
You are given an integer array candies where candies[i] is how many candies the i-th kid currently holds, plus an integer extraCandies. For each kid, determine whether giving that kid all of the extraCandies would leave them with the greatest candy count among all kids. Ties count: the kid only needs to reach the maximum, not exceed it.
Return a list of booleans, one per kid, where the i-th entry is True exactly when giving all the extra candies to kid i makes their total greater than or equal to every other kid’s current count.
Examples
- Input:
candies = [2, 3, 5, 1, 3], extraCandies = 3 → Output: [True, True, True, False, True]
Kid 0 reaches 5, tying the current max of 5; kid 3 only reaches 4, which is short.
- Input:
candies = [4, 2, 1, 1, 2], extraCandies = 1 → Output: [True, False, False, False, False]
Only kid 0 (4 + 1 = 5) can reach or beat the max of 4; nobody else gets past 3.
- Input:
candies = [12, 1, 12], extraCandies = 10 → Output: [True, False, True]
Kid 1 reaches 11, still below the max of 12.
Constraints
2 <= n <= 100 where n = len(candies)
1 <= candies[i] <= 100
1 <= extraCandies <= 50
The bounds are small, but the intended solution is O(n).
Think about it first
Hint 1
For a fixed kid, what single number about the rest of the array do you need to answer "would they have the greatest count"?
Hint 2
Comparing kid i against every other kid repeats the same work n times. Can something be computed once, before the loop?
Hint 3
Compute `m = max(candies)` once. Kid i's answer is whether `candies[i] + extraCandies >= m` — note the current max always belongs to some kid, so comparing against it is safe even for that kid.
TL;DR
Precompute the max once, then one comparison per kid — O(n) time, O(1) extra space (beyond the output).
Approach 1 — Brute force
For each kid, boost their count and compare against every other kid’s current count.
from typing import List
def kidsWithCandies(candies: List[int], extraCandies: int) -> List[bool]:
n = len(candies)
result = []
for i in range(n):
boosted = candies[i] + extraCandies
ok = True
for j in range(n):
if candies[j] > boosted:
ok = False
break
result.append(ok)
return result
Complexity: O(n²) time, O(1) extra space.
With n ≤ 100 this passes, but it recomputes the same maximum scan for every kid.
Approach 2 — Precompute the max
Kid i ties or beats everyone if and only if candies[i] + extraCandies reaches the array’s current maximum. That maximum is the same for every kid, so compute it once.
from typing import List
def kidsWithCandies(candies: List[int], extraCandies: int) -> List[bool]:
top = max(candies)
return [c + extraCandies >= top for c in candies]
Walkthrough on candies = [2, 3, 5, 1, 3], extraCandies = 3:
top = max(candies) = 5.
- Kid 0: 2 + 3 = 5 ≥ 5 →
True.
- Kid 1: 3 + 3 = 6 ≥ 5 →
True.
- Kid 2: 5 + 3 = 8 ≥ 5 →
True.
- Kid 3: 1 + 3 = 4 < 5 →
False.
- Kid 4: 3 + 3 = 6 ≥ 5 →
True.
Result: [True, True, True, False, True] — matches the expected output.
Complexity: O(n) time (one pass for max, one for the comparisons), O(1) extra space beyond the returned list.
Common pitfalls
- Using strict
> instead of >= — the problem asks for the greatest count, and ties with the current maximum qualify.
- Worrying that kid i is compared against their own value inside
max(candies): it’s harmless, since candies[i] + extraCandies >= candies[i] always holds (extraCandies ≥ 1).
- Recomputing
max(candies) inside the loop, which turns the O(n) solution back into O(n²).
Pattern takeaway
When a per-element question reduces to “how does this element compare to some aggregate of the whole array?”, compute the aggregate (max, min, sum, a count table) in one pass first, then answer each element in O(1). Moving a loop-invariant computation out of the loop is one of the most common array optimizations.