logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
13. Roman to Integer14. Longest Common Prefix20. Valid Parentheses26. Remove Duplicates from Sorted Array35. Search Insert Position69. Sqrt(x)70. Climbing Stairs88. Merge Sorted Array (In-Place)104. Maximum Depth of Binary Tree118. Pascal's Triangle121. Best Time to Buy and Sell Stock205. Isomorphic Strings242. Valid Anagram283. Move Zeroes345. Reverse Vowels of a String605. Can Place Flowers704. Binary Search796. Rotate String1071. Greatest Common Divisor of Strings1299. Replace Elements with Greatest Element on Right Side1431. Kids With the Greatest Number of Candies1614. Maximum Nesting Depth of the Parentheses1752. Check if Array Is Sorted and Rotated1768. Merge Strings Alternately1903. Largest Odd Number in String2965. Find Missing and Repeated Values
Code Scratch/leetcode/easy

1431. Kids With the Greatest Number of Candies

2 min read
On this pageIntuition
IntuitionApproachComplexityCode

Given an integer array, where candies[i] represents the number of candies the ithi_{th}ith​ kid has.

Return a boolean array result of length n, where result[i] is true if post giving the ithi_{th}ith​ kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.

Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true] 
Explanation: If give 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.

# Intuition

  • Find the kid with maximum candies & compare with each kid if given extra candies he will have the most or not.

# Approach

  • Iterate over the array to find the kid with maximum candies.
  • Iterate over the array again to check if candies[i] + extraCandies is greater or equal to the kid with maximum candies.

# Complexity

Space ComplexityTime Complexity
O(1)\text{O}(1)O(1)O(n)\text{O}(n)O(n)
  • Only extra space is used to store the result
  • We itreate over the array only once

# Code

class Solution {
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        int maxCandiesOnAKid = 0;
        for(int i = 0; i < candies.length; ++i) {
            maxCandiesOnAKid = Math.max(maxCandiesOnAKid,candies[i]);
        }
 
        List&lt;Boolean&gt; result = new ArrayList<&gt;();
        for(int i = 0; i &lt; candies.length; ++i) {
            result.add(candies[i] + extraCandies&gt;= maxCandiesOnAKid ); 
        }        
 
        return result;
    }
}
←Previous1299. Replace Elements with Greatest Element on Right SideNext→1614. Maximum Nesting Depth of the Parentheses

On this page

IntuitionApproachComplexityCode