-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGreatestNumberOfCandies.java
42 lines (27 loc) · 1.31 KB
/
GreatestNumberOfCandies.java
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
35
36
37
38
39
40
41
42
package array_string;
import java.util.ArrayList;
import java.util.List;
public class GreatestNumberOfCandies {
public List<Boolean> kidsWithCandies(int[] kidsCandies, int additionalCandies) {
List<Boolean> results = new ArrayList<>(kidsCandies.length);
int highestCandies = 0;
for (int candyCount : kidsCandies) {
highestCandies = Math.max(candyCount, highestCandies);
}
int threshold = highestCandies - additionalCandies;
for (int candyCount : kidsCandies) {
results.add(candyCount >= threshold);
}
return results;
}
public static void main(String[] args) {
GreatestNumberOfCandies solution = new GreatestNumberOfCandies();
List<Boolean> result1 = solution.kidsWithCandies(new int[]{2, 3, 5, 1, 3}, 3);
assert result1.equals(List.of(true, true, true, false, true)) : "Test case 1 failed";
List<Boolean> result2 = solution.kidsWithCandies(new int[]{4, 2, 1, 1, 2}, 1);
assert result2.equals(List.of(true, false, false, false, false)) : "Test case 2 failed";
List<Boolean> result3 = solution.kidsWithCandies(new int[]{12, 1, 12}, 10);
assert result3.equals(List.of(true, false, true)) : "Test case 3 failed";
System.out.println("All tests passed!");
}
}