Kids With the Greatest Number of Candies · Optimal Solution

One threshold. Every kid measured against it.

Picture an award ceremony where every child holding at least as many candies as the current leader qualifies for a prize. The host finds the top count once, then walks down the line asking a single question: "If I hand you extra candies, do you reach the leader?" Each answer is independent — one kid's outcome never changes another's. Find the maximum, sweep the array, compare each boosted total. O(n) time. O(n) space for the result.


Interactive

Watch each kid reach — or fall short


Concept

A fixed bar, not a moving target

The algorithm hinges on a single pre-computed threshold. Every evaluation is a self-contained arithmetic check — no kid's result depends on any other kid's.

fixed threshold

Scan the array once to find its maximum. This value becomes the bar every kid must meet. It never changes — one pass, one number, done.

hypothetical independence

Each kid is evaluated as if only they received the extra candies. The question is hypothetical and isolated — kid 0's answer has no bearing on kid 3's.

the ≥ check

The comparison is candies[i] + extra ≥ threshold, not strictly greater. A kid who ties the leader after the bonus still qualifies — equality counts.


Algorithm

Find the max, then sweep

Two passes over the array — one to establish the bar, one to evaluate every kid against it.

  1. 1

    Find the maximum

    Scan the entire array to compute threshold = candies.Max(). This is the highest candy count any kid currently holds.

  2. 2

    Allocate the result array

    Create a boolean array of the same length: result = new bool[candies.Length].

  3. 3

    Evaluate each kid

    For each index, compute candies[i] + extraCandies and compare against the threshold. Store true if the sum meets or exceeds it, false otherwise.

  4. 4

    Return the result

    The boolean array is the answer. No sorting, no second pass needed — each slot was decided in constant time.


Edge Cases

Where every kid wins — or only the leader

The threshold and the extra candy count together determine who qualifies. A few boundary scenarios are worth reasoning through.

All equal elements

When every kid holds the same number of candies, the threshold equals that value. Adding any positive extra guarantees every kid qualifies — the result is all true.

Single kid

With only one child, the threshold is their own count. Adding extra candies can only increase it — always true. No comparison needed, but the algorithm handles it naturally.

Leader-only qualification

When the extra amount is too small to close the gap for trailing kids, only the current leader(s) qualify. The further behind a kid is, the more extra candies they'd need.

Exact tie after bonus

A kid whose boosted total exactly equals the threshold still qualifies — the check is , not >. Missing this subtlety flips answers from true to false.