InterviewPrepKit

Home / Coding / Algorithm & Data Structure / Arrays & Hashing

Kids With the Greatest Number of Candies

easy Original ↗ 00:00

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, determine whether giving that kid all of the extraCandies would leave them with the greatest candy count among all kids. Ties count: the kid only needs to reach the maximum, not exceed 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 small, but the intended solution is O(n).

Think about it first

Hint 1 For a fixed kid, what single number about the rest of the array do you 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 whether `candies[i] + extraCandies >= m` — note the current max always belongs to some kid, so comparing against it is safe even for that kid.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug