Skip to content
Problem 1431 · LeetCode 1431(opens in a new tab)

Kids With the Greatest Number of Candies

EasyArrays

A group of kids each hold some candies: candies[i] is how many kid i has. You also hold extraCandies more, and you plan to hand all of them to a single kid.

For each kid, decide whether that kid would have the most candies of anyone (ties count) if they were the one who got all the extras. Return a list of booleans, one per kid, in the same order.

Example 1

Input:  candies = [4, 7, 1, 6], extraCandies = 2
Output: [false, true, false, true]
Why:    the top is 7; only 7 + 2 and 6 + 2 reach it
2 more examples and the constraints

Example 2

Input:  candies = [3, 3, 3], extraCandies = 1
Output: [true, true, true]
Why:    any kid with the extra beats the other two

Example 3

Input:  candies = [10, 2, 9], extraCandies = 1
Output: [true, false, true]
Why:    9 + 1 ties the 10, which still counts

Constraints

  • There are between 2 and 100 kids
  • Each kid holds between 1 and 100 candies
  • You have between 1 and 50 extra candies

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Compare with every kidO(n²)
  2. Compare with the maximumO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100 kidsVerdict
Compare with every kidO(n²)
Passes; n × n checks
Compare with the maximumO(n)
Best; two short passes

n = number of kids.

Edge cases to test

  • A tie with the leader. [5, 8] with 3 extra: 5 + 3 = 8 ties the top, so the answer is true.
  • All piles equal. [3, 3, 3]: every kid is already at the top, so every answer is true.
  • One candy short. [5, 9] with 3 extra: 5 + 3 = 8 < 9, so kid 0 gets false.
  • The leader. The kid with the most always gets true, since adding extras only helps.

Common mistakes

  • Using > instead of >=, which turns every tie into false.
  • Adding the extras to the maximum as well, as if every kid got them at once.
  • Recomputing the maximum inside the loop, which brings back the O(n²) cost.

How to talk through it

  1. 1Clarify. Do ties with the leader count as having the most? Does one kid get all the extras?
  2. 2Give the simple version. For each kid, compare their total against every other kid.
  3. 3Spot the one rival. Only the largest pile can beat anyone, so compare against that alone.
  4. 4Code it. One pass for best, then one pass checking candies[i] + extraCandies ≥ best.
  5. 5Test out loud. Run a tie with the leader, all equal piles, and a kid one candy short.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?