Watch 10 video solutions for Kids With the Greatest Number of Candies, a easy level problem involving Array. This walkthrough by Engineering Digest has 10,122 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.
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.
Note that multiple kids can have the greatest number of candies.
Example 1:
Input: candies = [2,3,5,1,3], extraCandies = 3 Output: [true,true,true,false,true] Explanation: If you give all extraCandies to: - Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids. - Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids. - Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids. - Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids. - Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
Example 2:
Input: candies = [4,2,1,1,2], extraCandies = 1 Output: [true,false,false,false,false] Explanation: There is only 1 extra candy. Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
Example 3:
Input: candies = [12,1,12], extraCandies = 10 Output: [true,false,true]
Constraints:
n == candies.length2 <= n <= 1001 <= candies[i] <= 1001 <= extraCandies <= 50Problem Overview: You are given an integer array candies where candies[i] represents the number of candies each kid has, and an integer extraCandies. For every kid, check if giving them all the extra candies would make their total at least as large as the current maximum among all kids. Return a boolean array indicating which kids could end up with the greatest number.
The key observation: a kid can have the greatest number of candies if candies[i] + extraCandies >= max(candies). The entire problem reduces to identifying the current maximum and comparing each value against it.
Approach 1: Brute Force Comparison (O(n²) time, O(1) space)
For each kid, temporarily assume they receive extraCandies. Then iterate through the entire array to verify whether any other kid still has more candies. If none do, that kid qualifies for the result array. This approach uses nested iteration: the outer loop selects a kid and the inner loop checks every other kid. No additional data structures are required beyond the result list, so auxiliary space remains O(1) (excluding output).
This method demonstrates the core logic clearly but performs unnecessary repeated scans of the array. Every candidate forces a full comparison against all other values. With n kids, that leads to n × n comparisons, resulting in O(n²) time complexity.
Approach 2: One-Pass Conditional Check (O(n) time, O(1) space)
A more efficient strategy computes the maximum value in the array first. Once you know maxCandies, the problem becomes a simple condition check: for each index i, evaluate candies[i] + extraCandies >= maxCandies. If true, that kid could reach or exceed the current maximum after receiving the extra candies.
The algorithm works in two lightweight passes. The first pass scans the array to find the maximum value. The second pass evaluates the condition for each kid and builds the boolean result list. Both passes are linear, so the total runtime is O(n), while space overhead remains O(1) aside from the output.
This pattern appears frequently in array problems: compute a global statistic (like maximum or minimum) and reuse it during a second traversal. Compared to naive nested comparisons, the improvement from O(n²) to O(n) is significant even for moderately sized inputs.
Recommended for interviews: Interviewers expect the maximum-scan strategy. The brute force approach shows you understand the condition being checked, but the optimized solution demonstrates awareness of redundant work and how to remove it. Recognizing when a global value (like a maximum) can simplify repeated comparisons is a common optimization pattern in array, brute-force, and greedy-style reasoning problems.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Comparison | O(n²) | O(1) | Good for understanding the raw condition and baseline logic when first approaching the problem. |
| One-Pass Conditional Check (Find Max + Compare) | O(n) | O(1) | Best general solution. Uses a maximum scan followed by a simple comparison for each element. |