Kids With the Greatest Number of Candies
Precompute Max + Scan
Asked at 6 companies
5 recent
- Meta
- Amazon
- Microsoft
- Bloomberg
- Wipro
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementThere are n kids with candies. You are given an integer array candies, where candies[i] is the number of candies the ith kid has, and an integer extraCandies.ReadHide
Problem Statement
There are n kids with candies. You are given an integer array candies, where candies[i] is the number of candies the ith kid has, and an integer extraCandies.
Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.
Multiple kids can have the greatest number of candies.
Input / Output
Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]
Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]
Constraints
n == candies.length
2 ≤ n ≤ 100
1 ≤ candies[i] ≤ 100
1 ≤ extraCandies ≤ 50
Understanding the Problem
- Giving extras to kid
idoesn't change anyone else → the bar to beat is the current max - Kid
iqualifies iffcandies[i] + extraCandies >= max(candies)(ties count)
Edge Cases
- Ties for greatest →
>=, not> - The kid who already has the max is always
true
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Precompute + Scan — compute the max once instead of re-scanning for every kid.
Approach
Brute Force — O(n²)
For each kid i:
check candies[i] + extra >= candies[j] for every j
Optimized — O(n)
mx = max(candies)
result[i] = candies[i] + extra >= mx
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Brute Force | O(n²) | O(1) | Re-scan all kids per kid |
| Optimized | O(n) | O(1) | Max once (output not counted) |
Online Compiler
Solution code
import sys
input = sys.stdin.readline
# ─────────────────────────────────────────────────────────────────────────
# BRUTE FORCE — O(n²) time | O(1) auxiliary space
# For each kid, compare against every other kid
# ─────────────────────────────────────────────────────────────────────────
def kids_with_candies_brute_force(candies, extra_candies):
result = []
for c in candies:
ok = True
for other in candies:
if c + extra_candies < other: # someone still has more → not greatest
ok = False
break
result.append(ok)
return result
# ─────────────────────────────────────────────────────────────────────────
# OPTIMIZED — O(n) time | O(1) auxiliary space
# Find the max once, then one comparison per kid
# ─────────────────────────────────────────────────────────────────────────
def kids_with_candies(candies, extra_candies):
mx = max(candies) # current greatest
return [c + extra_candies >= mx for c in candies] # ties count
# ── Input ─────────────────────────────────────────────────────────────────
n, extra_candies = map(int, input().split())
candies = list(map(int, input().split()))
ans = kids_with_candies(candies, extra_candies)
print("[" + ",".join("true" if x else "false" for x in ans) + "]")Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language