Kids With the Greatest Number of Candies (LeetCode 1431): Solution and Explanation

Easy
Array

Kid i has candies[i] candies and you hold extraCandies more. For each kid, decide whether giving them all the extra candies would leave them with at least as many as every other kid, and return those answers as a boolean array.

Intuition

It looks like you might need to re-check the whole group for every kid, which would be O(n^2). But giving the extra candies to one kid changes nobody else's count, so the number to beat is always the same: the current maximum.

Picture the candies as bars with a horizontal line drawn at the tallest bar. For each kid, stack the extra candies on top of their bar. If the stack reaches the line, that kid would have the most (a tie counts). If it stays below the line, someone still has more.

So the whole problem is one pass to find the maximum and one pass to compare candies[i] + extraCandies against it.

Approach: Compare each kid against the maximum

  1. Compute most = max(candies).
  2. Create an empty list result.
  3. For each count c in candies, append c + extraCandies >= most.
  4. Return result.

Step-by-step example

candies = [2,3,5,1,3], extraCandies = 3
  1. Scan for the maximum: 2, then 3, then 5. Nothing after kid 2 beats 5, so most = 5.
  2. Kid 0: 2 + 3 = 5, which reaches 5, so true.
  3. Kid 1: 3 + 3 = 6 >= 5, so true.
  4. Kid 2: 5 + 3 = 8 >= 5, so true.
  5. Kid 3: 1 + 3 = 4 < 5, so false.
  6. Kid 4: 3 + 3 = 6 >= 5, so true. The answer is [true,true,true,false,true].

Code: Python, Java, C++

1class Solution:
2    def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
3        most = max(candies)
4        result = []
5        for c in candies:
6            result.append(c + extraCandies >= most)
7        return result
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass finds the maximum and one pass builds the answer.

Space

O(1)

Apart from the returned list of n booleans, only the maximum is stored.

Other approaches

Brute force per kid (time O(n^2), space O(1))

For each kid, add the extra candies and compare against every other kid one by one. It is correct but repeats the same maximum search n times.

Threshold form (time O(n), space O(1))

Rewrite the test as c >= most - extraCandies. Compute the threshold once and compare each kid to it. It is the same algorithm, just with one subtraction instead of n additions.

Common mistakes

  • Using > instead of >=. A kid who ties the current maximum still has the greatest number, so ties must be true.
  • Updating the maximum while building the answer. The maximum must be fixed before the second pass, otherwise kids early in the array are compared against a smaller value.
  • Actually adding the extra candies into the array. Each kid is a separate what-if; mutating candies[i] would change the comparison for later kids if the maximum were recomputed.
  • Returning a list of the winning indices instead of a boolean for every kid.

Frequently asked questions

Why is the maximum the only thing that matters?

Only the chosen kid's count changes. Every other kid keeps their original count, so the largest of those is at most the original maximum, and comparing against the original maximum is enough.

Can more than one kid get true?

Yes. Each kid is considered separately, as if they alone received the extra candies, so many kids can reach the maximum. The kid who already has the maximum always gets true.

Does the answer ever depend on the order of the kids?

No. Each entry depends only on that kid's count, the extra candies and the global maximum, so reordering the input just reorders the output.