Knowledge Guide
HomeDSAArrays

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:

Example 2:

Example 3:

Constraints:

Try it yourself

Try solving this question here:

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

Code

java
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.
🤖 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.

🪜 Hint ladder (no spoilers)

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.
🎨 Explain the approach visually

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.
🔍 Review my solution

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.
🔁 Drill the pattern

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.

📝 My notes