Kids With the Greatest Number of Candies solution and explanation
9 more videos on this problem
leetcode 1431 solution ( Kids With the Greatest Number of Candies )Engineering Digest · 3:46 · 10K views
LeetCode 1431 - Kids With the Greatest Number of Candies - Python SolutionCode with Carter · 3:09 · 5K views
Kids With the Greatest Number of Candies - (Leetcode - 1431) - : Explanation ➕ Live CodingcodestorywithMIK · 5:48 · 5K views
Leetcode | 1431. Kids With the Greatest Number of Candies | Easy | Java SolutionDeveloper Docs · 5:27 · 4K views
1431. Kids With the Greatest Number of Candies (Leetcode Easy)Programming Live with Larry · 2:46 · 2K views
Kids With the Greatest Number of Candies - LeetCode 1431 - Python #leetcode #leetcode75Deepti Talesra · 5:22 · 2K views
Kids With the Greatest Number of Candies | LeetCode 1431 | CppCodeClips with Abhishek Ranjan · 5:13 · 1K views
LeetCode 1431. Kids with the Greatest Number of Candies (Java)Syntax · 5:38 · 332 views
Kids With the Greatest Number of Candies | LEETCODE - 1431 | Easy solutionhi-malik · 7:44 · 194 views
Leetcode Day 3 - Kids With the Greatest Number of Candies - Google Interview Question - #1431Algometica · 6:59 · 171 views
The problem
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 <= 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.
def kidsWithCandies(candies, extraCandies):
maxCandies = max(candies)
return [(candy + extraCandies) >= maxCandies for candy in candies]import java.util.ArrayList;
import java.util.List;
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int maxCandies = 0;
for (int candy : candies) {
maxCandies = Math.max(maxCandies, candy);
}
List<Boolean> result = new ArrayList<>();
for (int candy : candies) {
result.add(candy + extraCandies >= maxCandies);
}
return result;
}#include <vector>
std::vector<bool> kidsWithCandies(std::vector<int>& candies, int extraCandies) {
int maxCandies = *max_element(candies.begin(), candies.end());
std::vector<bool> result(candies.size());
for (int i = 0; i < candies.size(); i++) {
result[i] = (candies[i] + extraCandies >= maxCandies);
}
return result;
}function kidsWithCandies(candies, extraCandies) {
let maxCandies = Math.max(...candies);
return candies.map(candy => candy + extraCandies >= maxCandies);
}#include <stdbool.h>
void kidsWithCandies(int* candies, int candiesSize, int extraCandies, bool* result) {
int maxCandies = 0;
for (int i = 0; i < candiesSize; i++) {
if (candies[i] > maxCandies) {
maxCandies = candies[i];
}
}
for (int i = 0; i < candiesSize; i++) {
result[i] = (candies[i] + extraCandies >= maxCandies);
}
}using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public IList<bool> KidsWithCandies(int[] candies, int extraCandies) {
int maxCandies = candies.Max();
List<bool> result = new List<bool>();
foreach (int candy in candies) {
result.Add(candy + extraCandies >= maxCandies);
}
return result;
}
}The algorithm identifies the maximum number of candies using the built-in max function. It then uses list comprehension to form the result by verifying if each kid's candies plus extra candies would at least equal the maximum.
Time Complexity: O(n)
Space Complexity: O(n) for the result list.
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.
def kidsWithCandies(candies, extraCandies):
maxCandies = max(candies)
return [candy + extraCandies >= maxCandies for candy in candies]import java.util.ArrayList;
import java.util.List;
public class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int maxCandies = 0;
for (int candy : candies) {
maxCandies = Math.max(maxCandies, candy);
}
List<Boolean> result = new ArrayList<>(candies.length);
for (int candy : candies) {
result.add(candy + extraCandies >= maxCandies);
}
return result;
}
}#include <vector>
std::vector<bool> kidsWithCandies(std::vector<int>& candies, int extraCandies) {
int maxCandies = *max_element(candies.begin(), candies.end());
for (int i = 0; i < candies.size(); ++i) {
candies[i] = (candies[i] + extraCandies >= maxCandies);
}
return std::vector<bool>(candies.begin(), candies.end());
}function kidsWithCandies(candies, extraCandies) {
let maxCandies = Math.max(...candies);
return candies.map(candy => candy + extraCandies >= maxCandies);
}#include <stdbool.h>
void kidsWithCandies(int* candies, int candiesSize, int extraCandies, bool* result) {
int maxCandies = 0;
for (int i = 0; i < candiesSize; i++) {
if (candies[i] > maxCandies) {
maxCandies = candies[i];
}
}
for (int i = 0; i < candiesSize; i++) {
result[i] = candies[i] + extraCandies >= maxCandies;
}
}using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public IList<bool> KidsWithCandies(int[] candies, int extraCandies) {
int maxCandies = candies.Max();
List<bool> result = new List<bool>();
foreach (int candy in candies) {
result.Add(candy + extraCandies >= maxCandies);
}
return result;
}
}The solution leverages Python's efficient handling of list operations, where finding the maximum is followed by a single iteration to check each condition and populate the result in one forward-looking pass.
Time Complexity: O(n)
Space Complexity: O(n) for the result list.
Approach 3: Default Approach
function kidsWithCandies(candies: number[], extraCandies: number): boolean[] {
const max = candies.reduce((r, v) => Math.max(r, v));
return candies.map(v => v + extraCandies >= max);
}func kidsWithCandies(candies []int, extraCandies int) (ans []bool) {
mx := slices.Max(candies)
for _, candy := range candies {
ans = append(ans, candy+extraCandies >= mx)
}
return
}impl Solution {
pub fn kids_with_candies(candies: Vec<i32>, extra_candies: i32) -> Vec<bool> {
let max = *candies.iter().max().unwrap();
candies.iter().map(|v| v + extra_candies >= max).collect()
}
}class Solution {
/**
* @param Integer[] $candies
* @param Integer $extraCandies
* @return Boolean[]
*/
function kidsWithCandies($candies, $extraCandies) {
$max = max($candies);
$rs = [];
for ($i = 0; $i < count($candies); $i++) {
array_push($rs, $candies[$i] + $extraCandies >= $max);
}
return $rs;
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n) |
| One-Pass Conditional Check | Time Complexity: O(n) |
| Default Approach | – |
Complexity analysis
| 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. |
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.
