Problem Walkthrough

Greatest Candies LeetCode 1431 Guide

Find the current maximum candies any kid holds, then check whether each kid's count plus the extra candies reaches or exceeds that maximum — a clean O(n) boolean mapping.

6 min read|

Greatest Candies

LeetCode 1431

Problem Overview

LeetCode 1431 — Kids With the Greatest Number of Candies — gives you an integer array candies where candies[i] represents how many candies the i-th kid currently has, plus an integer extraCandies representing a bonus you can give to any one kid.

Your task is to return a boolean array result of the same length. result[i] is true if, after giving the i-th kid all extraCandies, that kid would have the greatest number of candies among all the kids — or at least tie for greatest.

The problem does not ask you to redistribute optimally or to figure out who deserves the candy. It simply asks: for each kid independently, if they received all the extras, could they reach the top?

  • Input: candies array (length 2–100, values 1–100) and integer extraCandies (1–50)
  • Output: boolean array of the same length as candies
  • result[i] = true if candies[i] + extraCandies >= max(candies)
  • Each kid is evaluated independently — the extra candies are not removed from the pool after one check
  • Ties count: reaching the current maximum is sufficient for a true result

The Simple Insight

The entire problem collapses to one observation: compute the current maximum number of candies any kid has, then for each kid ask whether their count plus extraCandies meets or beats that maximum.

There is no greedy optimization, no dynamic programming, and no sorting required. The maximum is a single value you compute once. Everything else is a straightforward boolean comparison across the array.

This pattern — compute a single aggregate, then map each element to a boolean using that aggregate — appears frequently in beginner array problems. Recognizing it immediately separates candidates who pattern-match from those still reaching for nested loops.

💡

One-Pass-After-Max Pattern

This is a one-pass-after-max problem: compute max in O(n), then map each element to a boolean comparison in O(n). Total time is O(n) and extra space is O(1) — the output array does not count toward space complexity.

Algorithm

The algorithm is deliberately simple: two passes over the array, each O(n). First find the maximum. Second, generate the result array by comparing each element plus extraCandies against that maximum.

No helper data structures are needed. The only state you carry between the two steps is the single integer maxCandies. This is as minimal as array algorithms get.

The simplicity is intentional — LeetCode 1431 is rated Easy and is designed to test whether you can identify the correct abstraction (the max) rather than over-engineer the solution.

  1. 1Step 1: Compute maxCandies = max(candies) — a single scan across the array
  2. 2Step 2: For each index i, set result[i] = (candies[i] + extraCandies >= maxCandies)
  3. 3Step 3: Return the result array

Why >= Not >

A subtle point that trips up first-time solvers: the comparison is >= (greater than or equal), not > (strictly greater than). The problem statement says "greatest number of candies among the kids," and that explicitly includes ties.

Consider the kid who already has the maximum number of candies. After receiving extraCandies, they trivially reach the max — their comparison is maxCandies + extraCandies >= maxCandies, which is always true. This correctly produces a true result for them.

Using strict > instead would incorrectly return false for any kid currently at the maximum (and any kid who can only tie, not surpass, the max). The >= operator is the semantically correct choice here.

ℹ️

Ties Count

This is >= not >: if the current max is 5 and a kid has 3 with 2 extra, 3+2=5 >= 5 is true — they TIE for greatest, which counts. The kid who already holds the max will always get a true result because extraCandies is at least 1.

Walk-Through Example

Take the canonical example from LeetCode: candies = [2, 3, 5, 1, 3] and extraCandies = 3. The maximum candy count among the five kids is 5 (held by kid at index 2).

Now evaluate each kid independently by adding extraCandies to their count and comparing against 5. Every kid gets the same bonus; the question is just whether their sum reaches the bar.

The result array is [true, true, true, false, true]. Four of the five kids can reach the maximum with the extra candies. Only the kid with 1 candy falls short: 1 + 3 = 4, which is less than 5.

  1. 1candies = [2, 3, 5, 1, 3], extraCandies = 3
  2. 2maxCandies = max([2, 3, 5, 1, 3]) = 5
  3. 3Kid 0: 2 + 3 = 5 >= 5 → true
  4. 4Kid 1: 3 + 3 = 6 >= 5 → true
  5. 5Kid 2: 5 + 3 = 8 >= 5 → true
  6. 6Kid 3: 1 + 3 = 4 >= 5 → false
  7. 7Kid 4: 3 + 3 = 6 >= 5 → true
  8. 8Result: [true, true, true, false, true]

Code Walkthrough — Python and Java

In Python, the solution collapses to a one-liner after the max computation: max_c = max(candies), then return [c + extra_candies >= max_c for c in candies]. The list comprehension maps each element to the boolean result in a single readable expression.

In Java, you can use a traditional for loop or the Streams API. The loop version: int maxC = Arrays.stream(candies).max().getAsInt(), then a for loop building a boolean array. With streams: Arrays.stream(candies).mapToObj(c -> c + extraCandies >= maxC).collect(Collectors.toList()). Both are O(n) time and O(1) extra space.

Neither implementation requires sorting, binary search, or any auxiliary data structure. The simplicity of the code reflects the simplicity of the algorithm — if your solution is longer than 10 lines, you are likely over-engineering it.

⚠️

Do Not Sort

Do not sort the array: sorting is O(n log n) and unnecessary. A single max() call gives what you need in O(n). Sorting would also destroy the original indices, breaking the output order requirement.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now