Kids With the Greatest Number of Candies: Find Max and Compare
LeetCode Kids With the Greatest Number of Candies (problem 1431) gives you an array candies and an integer extraCandies. Each entry candies[i] represents the number of candies the i-th kid has. For each kid, you need to determine whether giving them all the extra candies would make their total the greatest (or tied for greatest) among all the kids. Return a list of booleans.
For example, given candies = [2, 3, 5, 1, 3] and extraCandies = 3, kid 0 has 2 candies. If you give all 3 extra candies to kid 0, they would have 5, which ties the current maximum. So kid 0 gets true. Kid 3 has 1 candy, and 1 + 3 = 4, which is still below the max of 5. So kid 3 gets false.
Why this problem matters
This problem is rated easy for good reason, but it teaches a pattern that shows up everywhere: find a reference value first, then compare everything against it. The "find max then compare" structure appears in problems ranging from stock price analysis to temperature tracking. Getting comfortable with this two-phase approach on a simple problem like this means you will recognize it instantly in harder contexts.
The key insight
At first glance, you might think about trying every possible distribution of extra candies. But the problem says "there is a way to distribute" such that kid i ends up with the greatest number. The optimal strategy for any individual kid is obvious: give all the extra candies to that kid. You do not need to share them.
That means kid i can reach the greatest number if and only if candies[i] + extraCandies is at least as large as the current maximum in the array. You find the max once, then check each kid against it. Two passes through the array, or one pass to find the max followed by a list comprehension.
The solution
def kids_with_candies(candies: list[int], extra_candies: int) -> list[bool]:
max_candies = max(candies)
return [c + extra_candies >= max_candies for c in candies]
The first line scans the entire array to find the maximum candy count. The second line builds the result by checking each kid: can their count plus the extra reach or exceed that maximum? That is it. Two lines, two passes, no tricks.
Why give all the extra candies to one kid? Because you want to know if kid i can have the greatest number. The best case for kid i is getting every extra candy. If even that is not enough, no distribution will work. This is a greedy reasoning step that simplifies the problem dramatically.
Visual walkthrough
Step 1: Find the maximum candy count
Scan the array once. The largest value is 5.
Step 2: Kid 0 has 2 candies. Can they reach the max?
2 + 3 = 5, which equals the max. Result: true.
Step 3: Kid 3 has 1 candy. Can they reach the max?
1 + 3 = 4, which is less than 5. Result: false.
Step 4: Check all kids and build the result
Each kid is compared against the max. O(n) time, one pass after finding the max.
The walkthrough above shows the two-phase pattern clearly. First you identify the target (the max), then you compare every element against it. There is no sorting, no nested loops, and no extra data structures beyond the output list. Each kid is evaluated independently, which is what makes the list comprehension so natural here.
Complexity analysis
| Approach | Time | Space |
|---|---|---|
| Find max + compare | O(n) | O(n) |
You scan the array once to find the max (O(n)), then scan it again to build the boolean result list (O(n)). That gives you O(n) total time. The output list itself takes O(n) space, and there is no additional memory needed beyond it.
There is no way to do better than O(n) time here since you must examine every element at least once. And the output requires O(n) space by definition. This solution is optimal.
The building blocks
1. Finding the maximum in a single pass
The first step is computing the maximum of the array. In Python, max(candies) handles this in one call, but under the hood it is a single linear scan. This building block appears in any problem where you need a reference point: the highest stock price, the tallest building, the largest element in a row.
max_val = max(arr)
2. List comprehension with threshold comparison
The second step is comparing every element against a threshold and producing a boolean for each one. The list comprehension [c + extra >= threshold for c in arr] packs the entire comparison loop into a single expression. This pattern shows up whenever you need to classify elements as above or below some bar.
result = [val + bonus >= threshold for val in arr]
These two building blocks are independent. You could find the max in one loop and do the comparison in another, or combine them. The key is recognizing that the comparison step only needs a single precomputed value, not repeated scans.
Edge cases
- All kids have the same number of candies. Every kid can reach the max (they are already at it, and extra candies only help). The result is all
true. - Only one kid. They are trivially the greatest. Result:
[true]. - extraCandies is 0. Only kids who already have the maximum get
true. Everyone else getsfalse. - One kid has all the candies. The max is large, and most kids will not be able to reach it even with the extra. This is the case where you see a mix of
trueandfalsein the output.
From understanding to recall
This problem is so simple that most people read the solution once and move on. But the "find max then compare" pattern is worth drilling. Not because this specific problem is hard, but because the same two-phase structure appears in dozens of other problems where the reference value is harder to spot. If you can produce this solution from memory without hesitation, you will recognize the pattern faster when it shows up in a medium or hard problem with more moving parts.
Space out your reviews. Solve it today, then again in a few days, then a week later. Each repetition takes less time and builds the kind of automatic recall that makes interviews feel effortless.
Related posts
- Maximum Subarray - Another problem where finding a maximum drives the solution
- Contains Duplicate - Simple array scanning pattern
- Move Zeroes - Another easy array problem that teaches clean iteration
Once you have mastered the basics of array scanning and comparison, CodeBricks helps you lock in the patterns with spaced repetition. Instead of solving 300 random problems, you drill the building blocks that actually recur, so each new problem feels like a remix of something you already know.