- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathLargestFour.java
32 lines (26 loc) · 1.03 KB
/
LargestFour.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
importjava.util.*;
classMain {
publicstaticintLargestFour(int[] arr) {
// code goes here
List<Integer> integers = Arrays.stream(arr).boxed().toList();
returnintegers.stream().sorted(Collections.reverseOrder()).limit(4).mapToInt(i -> i).sum();
}
publicstaticintLargestFour2(int[] arr) {
// code goes here
intresult = 0;
for (inti = arr.length - 4; i < arr.length; i++) {
if (i >= 0) {
result += arr[i];
}
}
returnresult;
}
publicstaticvoidmain(String[] args) {
// keep this function call here
System.out.println(LargestFour(newint[]{1, 1, 1, -5}) == -2);
System.out.println(LargestFour(newint[]{0, 0, 2, 3, 7, 1}) == 13);
System.out.println(LargestFour(newint[]{4, 5, -2, 3, 1, 2, 6, 6}) == 21);
System.out.println(LargestFour(newint[]{1, 1, 0}) == 2);
System.out.println(LargestFour(newint[]{1000045}) == 1000045);
}
}