-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKidsWithTheGreatestNumberOfCandies.java
More file actions
34 lines (30 loc) · 1.16 KB
/
KidsWithTheGreatestNumberOfCandies.java
File metadata and controls
34 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package Algorithms.IntegerArray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 05 April 2025
*/
public class KidsWithTheGreatestNumberOfCandies {
public static void main(String[] args) {
int[] candies = {2,3,5,1,3};
int extraCandies = 3;
System.out.println(kidsWithCandies(candies, extraCandies));
}
public static List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int max = 0;
for (int candy : candies) max = Math.max(max, candy); // or for (int c: candies) if(max < c) max = c; --> for more faster
List<Boolean> result = new ArrayList<>(candies.length);
for (int candy : candies) result.add(candy + extraCandies >= max);
return result;
}
public List<Boolean> kidsWithCandiesUsingStreams(int[] candies, int extraCandies) {
int max = Arrays.stream(candies).max().getAsInt();
return Arrays.stream(candies)
.boxed()
.map(candy -> candy + extraCandies >= max)
.collect(Collectors.toList());
}
}