LeetCode #1431 Easy

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.

Constraints
  • 2 <= candies.length <= 100
  • 1 <= candies[i] <= 100
  • 1 <= extraCandies <= 50
array
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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).

1

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.

2

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.

3

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.

04

Solution & live demo

1class Solution:
2 def kidsWithCandies(self, candies, extraCandies):
3 max_candies = max(candies)
4 return [
5 candy + extraCandies >= max_candies
6 for candy in candies
7 ]
05

Common pitfalls

Recomputing the maximum inside the loop

✗ Wrong
return [c + extraCandies >= max(candies) for c in candies]
✓ Right
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

✗ Wrong
c + extraCandies > max_candies
✓ Right
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

✗ Wrong
candies[i] += extraCandies
max_candies = max(candies)
✓ Right
# 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.

06

Edge cases

Extras are zero

Only kids already holding the maximum answer true.

All kids have equal candies

Every kid meets the bar, so all answers are true.

One kid, e.g. [5]

That kid is the maximum and trivially reaches it, giving [true].

Ties at the top, e.g. [2,5,5,1]

Both kids at 5 answer true because the comparison uses >= rather than >.

Large extras

Every kid clears the bar and all answers are true.

07

Complexity

Time
O(n)
Space
O(n)
One pass to find the maximum, one to build the answer. The space is the output list itself.