#1431 Kids With the Greatest Number of Candies
the problem
In plain words
candies[i] is how many candies kid i has. For each kid, imagine giving them all extraCandies. Would they then have at least as many as every other kid? Return one true/false per kid as a List<Boolean>.
in candies = [2,3,5,1,3], extraCandies = 3
out [true,true,true,false,true]
Only kid 3 (1 + 3 = 4) stays below the current max of 5.
in candies = [4,2,1,1,2], extraCandies = 1
out [true,false,false,false,false]
in candies = [12,1,12], extraCandies = 10
out [true,false,true]
- 2 ≤ n ≤ 100
- 1 ≤ candies[i] ≤ 100
- 1 ≤ extraCandies ≤ 50
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternSimulation
"At least as many as everyone" only depends on the current maximum. Find it once, then each answer is one comparison.
solutions
From brute force to optimal
For each kid, check that no other kid has more than candies[i] + extra.
- For each i, loop over every j.
- If some candies[j] > candies[i] + extra → false.
- Otherwise true.
n kids, each compared with n kids.
Only the output list.
class Solution { public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) { List<Boolean> result = new ArrayList<>(candies.length); for (int i = 0; i < candies.length; i++) { boolean greatest = true; for (int j = 0; j < candies.length; j++) { if (candies[j] > candies[i] + extraCandies) { // someone still has more greatest = false; break; } } result.add(greatest); // autoboxed to Boolean } return result; }}The only kid you really have to match is the current leader.
- Pass 1: max = largest candies[i].
- Pass 2: add
candies[i] + extra >= maxfor each kid.
Two passes.
One int besides the output list.
class Solution { public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) { int max = 0; for (int c : candies) max = Math.max(max, c); // pass 1: the current record List<Boolean> result = new ArrayList<>(candies.length); // capacity known: no resizing for (int c : candies) result.add(c + extraCandies >= max); // pass 2: ties count return result; }}in the room
How to say it in the interview
talk track
- Clarify that ties count as "greatest".
- Say why one max is enough: the gift is imaginary, so the max never changes between kids.
traps
- Using
>instead of>=— ties must be true. - Returning
boolean[]: the method must returnList<Boolean>(boxed values).
what they ask next
Follow-ups
What if each gift were real and the candies stayed?
Then the max can change after every gift, so update it as you go — still one pass, but the order of kids now matters.
before you submit
Edge cases to test
- Ties count: equal to the max is true.
- The kid who already has the max is always true.
practise next
Similar problems
- #1672Richest Customer WealthEasyin Java Arrays & Strings
- #121Best Time to Buy and Sell StockEasyin Arrays & Strings