Kids With the Greatest Number of Candies
Kids With the Greatest Number of Candies: given each kid's candy count and a number of extra candies, return for each kid whether giving them all the extras would make them have the greatest number among all kids.
- 2 <= candies.length <= 100
- 1 <= candies[i] <= 100
- 1 <= extraCandies <= 50
Intuition
The bar never moves. Every kid is compared against the current maximum, and handing extras to one child cannot lower anyone else's count — so compute the maximum once, then ask each kid a single question: does candies[i] + extraCandies reach it? Two passes, no recomputation.
This is the 'compute an aggregate once, then compare each element against it' shape. The trap is recomputing the aggregate inside the loop. Whenever every element is measured against a global property — a maximum, a mean, a total — hoist that computation out and the problem becomes two clean passes.
Approach
Before reading on: decide whether the comparison bar changes as extras are handed out, and whether a kid who exactly ties the leader should answer true. Aim for O(n).
The maximum is fixed before any extras are given
Each kid is evaluated independently — the question is hypothetical for every child, so the extras are never actually distributed. That means the comparison target is the maximum of the original array, computed once before the second pass. It is tempting to think the bar rises as candies are handed out, but the problem asks each question in isolation against the untouched arrangement.
Why '>=' and not '>'
The problem says the greatest, not strictly greater than everyone. Ties count: if a kid reaches exactly the maximum, they share the top spot and the answer is true. So the test is candies[i] + extraCandies >= max_candies. This also means the kid who already holds the maximum always answers true, since adding a non-negative number keeps them at or above the bar.
Two linear passes
The first pass finds the maximum in O(n). The second builds the result list, testing each kid against that stored value in O(1). Total time is O(n) with O(n) space for the output — which is required, since the answer has one boolean per kid. Recomputing max(candies) inside the loop would silently turn this into O(n²); hoisting it out is the whole efficiency of the solution.
Solution & live demo
Common pitfalls
Recomputing the maximum inside the loop
return [c + extraCandies >= max(candies) for c in candies]
max_candies = max(candies) return [c + extraCandies >= max_candies for c in candies]
max(candies) is an O(n) scan, so calling it once per kid makes the solution O(n²). It gives the right answer while quietly doing a hundred times more work than needed.
Using strict greater-than
c + extraCandies > max_candies
c + extraCandies >= max_candies
'The greatest' includes ties. With >, the kid who already holds the maximum answers false — on [2,5,5,1] with 0 extras, both leaders would be wrongly reported as false.
Actually distributing the extras
candies[i] += extraCandies max_candies = max(candies)
# leave candies untouched; test hypothetically
Each question is independent and hypothetical. Mutating the array makes later kids compete against an inflated bar, so the answers depend on iteration order.
Edge cases
Only kids already holding the maximum answer true.
Every kid meets the bar, so all answers are true.
That kid is the maximum and trivially reaches it, giving [true].
Both kids at 5 answer true because the comparison uses >= rather than >.
Every kid clears the bar and all answers are true.