easy Kids With the Greatest Number of Candies
Problem Statement
There are n kids with candies. You are given a candies array containing integers, where candies[i] denotes the number of candies the ith kid has, and an integer extraCandies, represents the number of extra candies that you have.
Return a boolean array result of length n, where result[i] is true if, after giving all the extraCandies to the ith kid, he/she will have the maximum number of candies among all the kids, or false otherwise.
Note: Multiple kids can have the maximum number of candies.
Examples
Example 1:
- Input: candies = [7, 3, 9, 2, 4], extraCandies = 5
- Expected Output: [true, false, true, false, true]
- Justification: If you give all extraCandies to:
- Kid 1, they will have 7 + 5 = 12 candies, which is the maximum among the kids.
- Kid 2, they will have 3 + 5 = 8 candies, which is not the greatest among the kids.
- Kid 3, they will have 9 + 5 = 14 candies, which is the greatest among the kids.
- Kid 4, they will have 2 + 5 = 7 candies, which is not the greatest among the kids.
- Kid 5, they will have 4 + 5 = 9 candies, which is the greatest among the kids.
Example 2:
- Input: candies = [5, 8, 6, 4, 2], extraCandies = 3
- Expected Output: [true, true, true, false, false]
- Justification: Giving 3 extra candies to the first, second, and third kid will make their totals 8, 11, and 9 respectively, which are the highest. Other kids can't reach these totals.
Example 3:
- Input: candies = [1, 2, 3, 4, 5], extraCandies = 4
- Expected Output: [true, true, true, true, true]
- Justification: Giving 4 extra candies to each kid will make their totals 5, 6, 7, 8, and 9 respectively, which means they all can potentially have the highest number of candies.
Constraints:
n == candies.length2 <= n <= 1001 <= candies[i] <= 1001 <= extraCandies <= 50
Try it yourself
Try solving this question here:
After you try — Pattern Transfer
Pattern: LINEAR SCAN FOR GLOBAL EXTREMUM THEN MAP (threshold vs global max).
Recognition signals:
- Answer at each index depends only on a global max (or min), not on neighbors.
- You would re-scan the whole array for every index if you were not careful (Θ(n²) smell).
- Boolean or “yes/no per element” after one global fact is known.
Template: m = max(candies); then for each i: result[i] = (candies[i] + extra >= m).
When NOT:
- Need running min/max for a one-transaction optimum (Stock I) → one-pass running min, not full max then map.
- Need k-th largest / top-k → heap or quickselect.
- Not two-pointers, not sliding window, not prefix sums.
Edge checklist
- All equal:
candies=[5,5,5], extra=0→[true,true,true](every kid is already max). - Extra=0: only kids already at the max are true.
- One far-ahead kid + small extra: only that kid (and any ties) true.
- Constraints:
n ≥ 2so empty/single N/A here.
Drill: Why is one pass insufficient if you refuse to store the array? (Each result needs the global max, which may appear after the current index.)
✅ Solution Kids With the Greatest Number of Candies
Problem Statement
There are n kids with candies. You are given a candies array containing integers, where candies[i] denotes the number of candies the ith kid has, and an integer extraCandies, represents the number of extra candies that you have.
Return a boolean array result of length n, where result[i] is true if, after giving all the extraCandies to the ith kid, he/she will have the maximum number of candies among all the kids, or false otherwise.
Note: Multiple kids can have the maximum number of candies.
Examples
Example 1:
- Input: candies = [7, 3, 9, 2, 4], extraCandies = 5
- Expected Output: [true, false, true, false, true]
- Justification: If you give all extraCandies to:
- Kid 1, they will have 7 + 5 = 12 candies, which is the maximum among the kids.
- Kid 2, they will have 3 + 5 = 8 candies, which is not the greatest among the kids.
- Kid 3, they will have 9 + 5 = 14 candies, which is the greatest among the kids.
- Kid 4, they will have 2 + 5 = 7 candies, which is not the greatest among the kids.
- Kid 5, they will have 4 + 5 = 9 candies, which is the greatest among the kids.
Example 2:
- Input: candies = [5, 8, 6, 4, 2], extraCandies = 3
- Expected Output: [true, true, true, false, false]
- Justification: Giving 3 extra candies to the first, second, and third kid will make their totals 8, 11, and 9 respectively, which are the highest. Other kids can't reach these totals.
Example 3:
- Input: candies = [1, 2, 3, 4, 5], extraCandies = 4
- Expected Output: [true, true, true, true, true]
- Justification: Giving 4 extra candies to each kid will make their totals 5, 6, 7, 8, and 9 respectively, which means they all can potentially have the highest number of candies.
Constraints:
n == candies.length2 <= n <= 1001 <= candies[i] <= 1001 <= extraCandies <= 50
Solution
To solve this problem, we first need to find the maximum number of candies that any kid currently has. Then, for each kid, we check if giving them all the extra candies would make their total number of candies greater than or equal to this maximum value. This approach ensures that we only need to traverse the list of candies twice, making it efficient.
This method works because it directly addresses the problem's requirement by focusing on the condition needed to determine if a kid can have the highest number of candies. By first finding the current highest number of candies, we simplify the subsequent checks for each kid, ensuring the solution is both straightforward and efficient.
Step-by-Step Algorithm
- Find the maximum number of candies any kid currently has.
- Create an empty list to store the results.
- For each kid, calculate their total candies if they receive all the extra candies.
- Compare this total to the maximum number of candies.
- If the total is greater than or equal to the maximum, add true to the result list.
- Otherwise, add false to the result list.
- Return the result list.
Algorithm Walkthrough
Input: candies = [7, 3, 9, 2, 4], extraCandies = 5
- Find the maximum candies:
maxCandies = 9 - Initialize result list:
result = [] - For each kid:
- Kid 1: 7 + 5 = 12 >= 9 (true)
- Kid 2: 3 + 5 = 8 < 9(false)
- Kid 3: 9 + 5 = 14 >= 9(true)
- Kid 4: 2 + 5 = 7 < 9(false)
- Kid 5: 4 + 5 = 9 >= 9(true)
- Return:
[true, false, true, false, true]
Code
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
// Find the maximum number of candies any kid currently has
int maxCandies = 0;
for (int candy : candies) {
if (candy > maxCandies) {
maxCandies = candy;
}
}
// Create a list to store the result
List<Boolean> result = new ArrayList<>();
for (int candy : candies) {
// Check if giving the current kid all extra candies makes their total the highest
result.add(candy + extraCandies >= maxCandies);
}
return result;
}
public static void main(String[] args) {
Solution solution = new Solution();
// Example 1
int[] candies1 = { 7, 3, 9, 2, 4 };
int extraCandies1 = 5;
System.out.println(solution.kidsWithCandies(candies1, extraCandies1)); // [true, false, true, false, true]
// Example 2
int[] candies2 = { 5, 8, 6, 4, 2 };
int extraCandies2 = 3;
System.out.println(solution.kidsWithCandies(candies2, extraCandies2)); // [true, true, true, false, false]
// Example 3
int[] candies3 = { 1, 2, 3, 4, 5 };
int extraCandies3 = 4;
System.out.println(solution.kidsWithCandies(candies3, extraCandies3)); // [true, true, true, true, true]
}
}
Complexity Analysis
Time Complexity
- Finding the maximum number of candies in the list takes
time, where n is the number of kids. - Checking each kid's candies after adding the extra candies also takes
time. - Therefore, the overall time complexity is
.
Space Complexity
- The space complexity is
due to the space required to store the result list, which has the same length as the input list of candies.
Pattern Transfer — LINEAR SCAN FOR GLOBAL EXTREMUM THEN MAP
Pattern name: find global max, then map each element against a threshold.
Recognition signals:
- Per-index answer depends only on global max/min (here:
candies[i] + extra ≥ max). - Brute nested “for each kid re-check everyone” smells like Θ(n²).
- Multiple kids may all be “true” (ties allowed) — no uniqueness constraint.
Template:
m = max(a)in one linear scan.- For each
x: emitx + extra ≥ m.
When NOT:
- Running optimum from left (Stock I) → keep
minSoFarin one pass. - k-th largest → heap / quickselect.
- Range aggregates → prefix sums; pair on sorted data → two pointers; constraint window → sliding window.
Complexity derivation (K11)
Pass 1: n comparisons to find max → Θ(n). Pass 2: n comparisons writing booleans → Θ(n). Total time Θ(n). Space: result list length n → Θ(n) required output; O(1) auxiliary beyond output. You cannot stream results without storing candies (or the max) because each answer depends on the global max that may appear later.
Edge hand-runs
- Normal:
[7,3,9,2,4]+5, max=9 →[T,F,T,F,T](7+5≥9, 3+5=8<9, …, 4+5=9≥9). - All equal:
[2,2,2]+1→ all true (2+1≥2). - Extra=0:
[1,5,3]+0→[false,true,false]. - Constraints remove n=0/1 (here
n≥2).
Drill: If extra could be negative, does the template still work? (Yes — threshold math is unchanged; constraints just forbid it.)
Recognize it: Scan once tracking what you need (running max/sum), or precompute a prefix-sum / hash → turn O(n²) into O(n).
▶ Visualize this problem (step it, predict each fork)
🤖 Don't fully get this? Learn it with Claude
Stuck on Kids With the Greatest Number of Candies? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Kids With the Greatest Number of Candies** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Kids With the Greatest Number of Candies** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Kids With the Greatest Number of Candies**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Kids With the Greatest Number of Candies**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.