LeetCode #1431 Easy

Kids With the Greatest Number of Candies

Kids With the Greatest Number of Candies is LeetCode 1431 (Easy). candies[i] is how many candies kid i has, and you hold extraCandies more. For each kid, imagine giving them all the extra candies. Return a list of booleans: true if that kid would then have at least as many candies as every other kid.

  • Each kid is a separate "what if". The extras are never split, and the other kids keep exactly what they had.
  • A tie for the most counts as having the greatest number.
Constraints
  • 2 <= n <= 100 (n is the number of kids)
  • 1 <= candies[i] <= 100
  • 1 <= extraCandies <= 50
array
Open on LeetCode ↗
02

Intuition

When kid i gets the extras, nobody else changes. So kid i ends up with the most exactly when candies[i] + extraCandies is at least the largest count among the other kids.

Compare every kid against the current maximum of the whole array. Using the whole array instead of "everyone except kid i" is safe: if kid i is the one holding the maximum, they already reach it before any extras. Find the maximum in one pass, then answer every kid with one addition and one comparison.

How to spot this pattern

Every element is checked against the same fixed reference value, here the largest count. Compute the reference once before the loop instead of rebuilding it for each element.

03

Approach

Try it first

Before reading on, take candies = [3, 7, 2] and extraCandies = 4. Decide what kid 0 should get, and whether your comparison uses > or >=.

1

Find the most candies any kid has

Scan the array once and keep the largest value. This is the bar every kid has to reach, and it does not move, because giving extras to one kid is only imagined and never changes the array.

2

Give each kid the extras and compare

For kid i, test candies[i] + extraCandies >= most. Use >=, because the problem treats a kid who ties for the most as having the greatest number.

3

Collect the answers in order

Append one boolean per kid, in the same order as candies, so position i of the result answers the question for kid i.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def kidsWithCandies(
▶3 self, candies: List[int], extraCandies: int
▶4 ) -> List[bool]:
▶5 most = max(candies)
▶6 return [count + extraCandies >= most for count in candies]
23513most candiesfirst find the most candies anyone has
extraCandies3
Each bar is one kid's candies. Before any kid can be judged, we need the bar they must reach: the largest count in the whole array. It does not change later, because the extras are only imagined.
23513most candies2kid 0 has 2: the most so far
kid0
most2
The first kid sets the starting bar: 2.
23513most candies3kid 1 has 3, more than 2 → new most
kid1
most3
Kid 1 beats the old best of 2, so the dashed line moves up to 3.
23513most candies5kid 2 has 5, more than 3 → new most
kid2
most5
Kid 2 beats the old best of 3, so the dashed line moves up to 5.
23513most candies5kid 3 has 1, not more than 5
kid3
most5
Kid 3 has 1, which does not beat 5, so the line stays where it is.
23513most candies5kid 4 has 3, not more than 5
kid4
most5
Kid 4 has 3, which does not beat 5, so the line stays where it is.
+32true3513most candies5kid 0 with 3 extra has 5: ties the most → true
kid0
with extras5
answertrue
Give kid 0 all 3 extra candies: 2 becomes 5. That equals the most, 5, and a tie for the most still counts as the greatest number, which is why the test is >=. The other kids keep their own counts.
2true+33true513most candies5kid 1 with 3 extra has 6: reaches 5 → true
kid1
with extras6
answertrue
Give kid 1 all 3 extra candies: 3 becomes 6. That is at least 5, so nobody would have more than this kid. The other kids keep their own counts.
2true3true+35true13most candies5kid 2 with 3 extra has 8: reaches 5 → true
kid2
with extras8
answertrue
Give kid 2 all 3 extra candies: 5 becomes 8. That is at least 5, so nobody would have more than this kid. The other kids keep their own counts.
2true3true5true+31false3most candies5kid 3 with 3 extra has 4: below 5 → false
kid3
with extras4
answerfalse
Give kid 3 all 3 extra candies: 1 becomes 4. That is still below 5, so the kid with 5 would have more. The other kids keep their own counts.
2true3true5true1false+33truemost candies5kid 4 with 3 extra has 6: reaches 5 → true
kid4
with extras6
answertrue
Give kid 4 all 3 extra candies: 3 becomes 6. That is at least 5, so nobody would have more than this kid. The other kids keep their own counts.
2true3true5true1false3truemost candies5answer: 4 of 5 kids can have the most
true4
false1
One boolean per kid, in the same order as the input. Every check used the same bar, 5, found once at the start.
05

Common pitfalls

Using > so that ties fail

✗ Wrong
count + extraCandies > most
✓ Right
count + extraCandies >= most

A kid who draws level with the leader does have the greatest number. With candies = [2, 3, 5, 1, 3] and extraCandies = 3, kid 0 reaches 2 + 3 = 5, the same as the leader, so the answer starts with true; with > it starts with false.

06

Edge cases

Every kid has the same number

Each kid already equals the maximum, so every answer is true no matter how many extras there are.

07

Complexity

Time
O(n)
Space
O(1)
One pass finds the maximum and one pass builds the answers. Apart from the output list, only the maximum is stored.
08

Kids With the Greatest Number of Candies FAQ

Can the answer be built in a single pass?

Not left to right without going back. Kid 0's answer depends on kids that come later, since one of them might hold the maximum. You need the maximum first, so two passes over the array is the natural minimum.

Do the extra candies get shared between kids?

No. Each kid is asked about separately, as if all of extraCandies went to that one kid and nobody else got anything. That is why the bar stays fixed at the original maximum for every kid.