Skip to main content

Kids With the Greatest Number of Candies - Solution & Explanation

EasyArray11 min readAsked at: Amazon, Microsoft, Meta +3
Practice this problem

Problem Statement

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.length
  • 2 <= n <= 100
  • 1 <= candies[i] <= 100
  • 1 <= extraCandies <= 50

Approach Overview

Problem 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 1: Brute Force Approach

This approach iterates through each kid's candies and checks if adding the extraCandies to their current candies makes them equal to or greater than the maximum number of candies present initially. It involves finding the maximum candies first and then performing this computation.

The solution begins by finding the current maximum number of candies in the array candies. Then, it iterates through each kid's candies, adding extraCandies and comparing the sum to the maximum. If the sum is greater than or equal to the maximum, true is stored in the result, otherwise false is stored.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of kids.
Space Complexity: O(1), not counting the output array.

Try this approach in the editor →

Approach 2: One-Pass Conditional Check

This approach optimizes the process by combining the finding of the max candies and constructing the result array into a single pass by keeping track of the maximum with conditional updates.

This C solution works within two concise passes, determining the maximum candies in the first loop and checking the condition for each child in the second loop without re-evaluating the maximum.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(1) aside from the result.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

PHP

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Brute Force Approach

Time Complexity: O(n), where n is the number of kids.
Space Complexity: O(1), not counting the output array.

One-Pass Conditional Check

Time Complexity: O(n)
Space Complexity: O(1) aside from the result.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force ComparisonO(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.

Video Solution

leetcode 1431 solution ( Kids With the Greatest Number of Candies )Engineering Digest10,436 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Kids With the Greatest Number of Candies easy or hard?
Kids With the Greatest Number of Candies is classified as an Easy problem on LeetCode. It focuses on array traversal and simple comparisons, making it a common introductory question for practicing basic algorithmic reasoning.
Kids With the Greatest Number of Candies Python/Java solution
In both Python and Java, the solution follows the same pattern: compute the maximum value in the candies array, then build a boolean list by checking whether candies[i] + extraCandies >= maxCandies. This implementation runs in O(n) time and uses constant auxiliary space.
How to solve Kids With the Greatest Number of Candies in O(n)?
First iterate through the array to compute maxCandies. Then iterate again and check the condition candies[i] + extraCandies >= maxCandies for each index. Append the boolean result to the output array. Both passes are linear, so the total complexity remains O(n).
What is the best approach for Kids With the Greatest Number of Candies?
The optimal approach first finds the maximum value in the candies array, then checks for each kid whether candies[i] + extraCandies is at least that maximum. This reduces the problem to two linear passes. The total time complexity is O(n) with O(1) extra space, making it the expected interview solution.
Is Kids With the Greatest Number of Candies asked at Google/Amazon/Meta?
This problem is categorized as an easy array question and is commonly used by companies like Amazon and Google for early interview rounds or online assessments. It tests basic iteration, conditional logic, and recognizing opportunities to avoid unnecessary nested loops.
What data structure is used in Kids With the Greatest Number of Candies?
The primary data structure is a simple array. The algorithm performs sequential scans of the array to compute the maximum value and evaluate a condition for each element. No advanced structures such as heaps or hash maps are required.
What is the time complexity of Kids With the Greatest Number of Candies?
The optimal solution runs in O(n) time because the array is scanned once to compute the maximum and once more to evaluate each kid. The brute force alternative compares every kid against all others, which leads to O(n²) time complexity.

Ready to solve this problem?

Practice Kids With the Greatest Number of Candies with our built-in code editor and test cases.

Practice on FleetCode