InterviewPrepKit

Home / Coding / Arrays & Hashing

Kids With the Greatest Number of Candies

easy Original β†—
Solving tips
  • Recognize the per-element question is really 'compare to an aggregate of the whole array' β€” here the array's maximum.
  • Compute m = max(candies) once, then each kid's answer is simply candies[i] + extraCandies >= m; O(n) time, O(1) extra space.
  • Pitfall: use >= not > since ties with the current max qualify (they only need to reach the greatest).
  • Pitfall: don't recompute max inside the loop, which silently makes it O(n^2).

Problem

You are given an integer array candies where candies[i] is how many candies the i-th kid currently holds, plus an integer extraCandies. For each kid, decide: if you handed that kid all of the extraCandies, would they then have the greatest candy count among all kids? (Ties count β€” they only need to reach the maximum, not beat it.)

Return a list of booleans, one per kid, where the i-th entry is True exactly when giving all the extra candies to kid i makes their total greater than or equal to every other kid’s current count.

Examples

  • Input: candies = [2, 3, 5, 1, 3], extraCandies = 3 β†’ Output: [True, True, True, False, True] Kid 0 reaches 5, tying the current max of 5; kid 3 only reaches 4, which is short.
  • Input: candies = [4, 2, 1, 1, 2], extraCandies = 1 β†’ Output: [True, False, False, False, False] Only kid 0 (4 + 1 = 5) can reach or beat the max of 4; nobody else gets past 3.
  • Input: candies = [12, 1, 12], extraCandies = 10 β†’ Output: [True, False, True] Kid 1 reaches 11, still below the max of 12.

Constraints

  • 2 <= n <= 100 where n = len(candies)
  • 1 <= candies[i] <= 100
  • 1 <= extraCandies <= 50

The bounds are tiny, but the intended solution is still the clean O(n) one.

Think about it first

Hint 1 For a fixed kid, what single number about the rest of the array do you actually need to answer "would they have the greatest count"?
Hint 2 Comparing kid i against every other kid repeats the same work n times. Can something be computed once, before the loop?
Hint 3 Compute `m = max(candies)` once. Kid i's answer is simply whether `candies[i] + extraCandies >= m` β€” note the current max always belongs to some kid, so comparing against it is safe even for that kid.
Your workspace Not runnable by design β€” this is your interview scratchpad. Saved on this device.