Domain 1EasyCORE

Kids With the Greatest Number of Candies

The problem asks us to determine for each kid in an array whether they can have the greatest number of candies after potentially distributing some extra candies among them. The input consists of two parts: an array `candies` where each element represents the number of candies a kid currently has, and an integer `extraCandies` representing the total number of additional candies that can be distributed. The output should be a boolean list where each element indicates whether the corresponding kid can have the greatest number of candies.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Array

1431. Kids With the Greatest Number of Candies

Model: qwen2.5-coder:14b
Topics: Array
FAANG asked: 0
Acceptance rate: 88.2%


Problem Breakdown

The problem asks us to determine for each kid in an array whether they can have the greatest number of candies after potentially distributing some extra candies among them. The input consists of two parts: an array candies where each element represents the number of candies a kid currently has, and an integer extraCandies representing the total number of additional candies that can be distributed. The output should be a boolean list where each element indicates whether the corresponding kid can have the greatest number of candies.

Inputs:

  • candies: An array of integers where each integer represents the number of candies a kid has.
  • extraCandies: An integer representing the total number of extra candies available for distribution.

Outputs:

  • A list of boolean values, where each value indicates whether the corresponding kid can have the greatest number of candies after distributing the extra candies.

Implicit Constraints or Edge Cases:

  • The array candies will have at least 2 and at most 100 elements.
  • Each element in candies will be between 1 and 100, inclusive.
  • extraCandies will be between 1 and 50, inclusive.

Intuition

To solve this problem, we need to determine if each kid can have the greatest number of candies after potentially receiving some or all of the extra candies. A brute-force approach would involve checking for each kid if there exists a distribution of extra candies such that they end up with the maximum number of candies. However, this approach is inefficient because it involves multiple iterations and complex logic.

A more efficient approach is to first find the maximum number of candies any kid currently has. Then, for each kid, we check if adding all the extraCandies to their current count would result in a value greater than or equal to this maximum. This way, we only need to traverse the array twice: once to find the maximum and another to compare each kid’s potential total candies.

Approach

  1. Find the Maximum Candies: Traverse the candies array to determine the maximum number of candies any kid currently has.
  2. Check Each Kid: For each kid in the candies array, check if adding all extraCandies to their current count results in a value greater than or equal to the maximum found in step 1.
  3. Return Result: Construct and return a list of boolean values indicating whether each kid can have the greatest number of candies.

Data Structures:

  • We will use an integer variable to store the maximum number of candies found.
  • We will use a list of booleans to store the result for each kid.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the candies array. This is because we traverse the array twice: once to find the maximum and another to check each kid’s potential total candies.
  • Space Complexity: O(n), where n is the length of the candies array, due to the storage required for the result list.

Solution

def kidsWithCandies(candies, extraCandies):
    # Step 1: Find the maximum number of candies any kid currently has
    max_candies = max(candies)
    
    # Step 2: Check each kid if they can have the greatest number of candies
    result = [candy + extraCandies >= max_candies for candy in candies]
    
    # Step 3: Return the result list
    return result

Edge Cases Handled

  • All kids have the same number of candies: The algorithm correctly identifies that all kids can have the greatest number of candies.
  • One kid has significantly more candies than others: The algorithm correctly identifies that only this kid can have the greatest number of candies even after distributing extra candies.
  • No extra candies available: The algorithm correctly identifies that no kid can have the greatest number of candies if extraCandies is 0.