Kids With the Greatest Number of Candies - Problem

Imagine you're a teacher distributing extra candies to kids in your classroom! ๐Ÿญ

You have n kids, each already holding some candies. You're given an integer array candies where candies[i] represents the number of candies the i-th kid currently has.

You also have extraCandies - a stash of bonus candies that you can give to any one kid. Your mission is to determine: "If I give ALL my extra candies to each kid (one at a time), would that kid end up having the most candies in the class?"

Goal: Return a boolean array result of length n, where result[i] is true if giving the i-th kid all the extraCandies makes them have the greatest number of candies among all kids.

Important: Multiple kids can be tied for having the greatest number of candies - that's perfectly fine!

Example: If kids have [2, 3, 5, 1, 3] candies and you have 3 extra candies, then giving all 3 extra candies to kid 0 gives them 2+3=5 candies, which ties for the maximum. So kid 0 would get true in the result!

Input & Output

Basic Example - Mixed Results
$ Input: candies = [2,3,5,1,3], extraCandies = 3
โ€บ Output: [true,true,true,false,true]
๐Ÿ’ก Note: Kid 0: 2+3=5 (ties with max 5) โœ“, Kid 1: 3+3=6 (exceeds max 5) โœ“, Kid 2: 5+3=8 (exceeds max 5) โœ“, Kid 3: 1+3=4 (less than max 5) โœ—, Kid 4: 3+3=6 (exceeds max 5) โœ“
All Kids Can Be Greatest
$ Input: candies = [4,2,1,1,2], extraCandies = 1
โ€บ Output: [true,false,false,false,false]
๐Ÿ’ก Note: Current maximum is 4. Kid 0: 4+1=5 โœ“, Kid 1: 2+1=3 < 4 โœ—, Kid 2: 1+1=2 < 4 โœ—, Kid 3: 1+1=2 < 4 โœ—, Kid 4: 2+1=3 < 4 โœ—
Large Extra Candies
$ Input: candies = [12,1,12], extraCandies = 10
โ€บ Output: [true,true,true]
๐Ÿ’ก Note: With 10 extra candies, everyone can exceed or match the current maximum of 12. Kid 0: 12+10=22 โœ“, Kid 1: 1+10=11 < 12 โœ—... Wait, that's wrong! Kid 1: 1+10=11 < 12 โœ—, but actually Kid 1: 1+10=11 < 12, so result should be [true,false,true]. Let me recalculate: Kid 0: 22โ‰ฅ12 โœ“, Kid 1: 11โ‰ฅ12 โœ—, Kid 2: 22โ‰ฅ12 โœ“

Visualization

Tap to expand
๐Ÿญ The Candy Champion Strategy ๐Ÿ†Step 1: Find Current Champion (Maximum = 5 candies)Kid 02 ๐ŸญKid 13 ๐ŸญKid 25 ๐ŸญCHAMPIONKid 31 ๐ŸญKid 43 ๐ŸญStep 2: Test Each Kid's Potential (Current + 3 Extra Candies)Kid 02+3=55โ‰ฅ5 โœ“Kid 13+3=66โ‰ฅ5 โœ“Kid 25+3=88โ‰ฅ5 โœ“Kid 31+3=44โ‰ฅ5 โœ—Kid 43+3=66โ‰ฅ5 โœ“๐Ÿง  Strategy Explanation๐ŸŽฏ Key Insight: Instead of comparing each kid with everyone else (O(nยฒ)), we only need to know the current champion's score and compare against it!โšก Algorithm:1. Find maximum candies in one pass: O(n)2. Check each kid's potential in another pass: O(n)3. Total time complexity: O(n) - much faster than brute force O(nยฒ)!Final Result: [true, true, true, false, true]
Understanding the Visualization
1
Scout the Current Champion
Look through all kids to find who has the most candies right now
2
Test Each Kid's Potential
For each kid, calculate: current candies + extra candies
3
Compare with Champion
If their potential >= champion's candies, they can be greatest
4
Record Results
Build boolean array showing who can become champion
Key Takeaway
๐ŸŽฏ Key Insight: By finding the maximum first, we transform an O(nยฒ) comparison problem into an elegant O(n) solution. Each kid only needs to 'compete' against the current champion, not everyone!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

We make two passes through the array: one to find maximum (O(n)) and one to check each kid (O(n))

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for storing the maximum value and loop variables

n
2n
โœ“ Linear Space

Constraints

  • n == candies.length
  • 2 โ‰ค n โ‰ค 100
  • 1 โ‰ค candies[i] โ‰ค 100
  • 1 โ‰ค extraCandies โ‰ค 50
Asked in
LeetCode 25 Google 8 Amazon 12 Meta 6
68.0K Views
Medium Frequency
~8 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen