Array / String· 15 min

Kids With the Greatest Number of Candies

LeetCode #1431 · Easy · Interactive step-by-step visualization

Setup

Example inputcandies = [2, 3, 5, 1, 3], extra = 3
Step 1 / 20

Current step

1 / 20

Find the current greatest candy count before distributing the 3 extra candies.

Step log

Extra candies: 3Current maximum: —

Children

2
0
3
1
5
2
1
3
3
4
Active
Comparing
Committed
Rejected or blocked
kids-with-the-greatest-number-of-candies.java
1public List<Boolean> kidsWithCandies(int[] candies, int extra) {
2 int maximum = 0;
3 for (int candy : candies) maximum = Math.max(maximum, candy);
4 List<Boolean> result = new ArrayList<>();
5 for (int candy : candies)
6 result.add(candy + extra >= maximum);
7 return result;
8}
● Java
Time
O(n)
Space
O(n)
Pattern
array scan
Frames
20