-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion3.java
More file actions
32 lines (28 loc) · 1.18 KB
/
Copy pathQuestion3.java
File metadata and controls
32 lines (28 loc) · 1.18 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
package practice4_sort;
import java.util.Arrays;
public class Question3 {
public int solution(int[] nums, int k){
int answer = 0;
int n = nums.length;
Integer[] tmp = Arrays.stream(nums).boxed().toArray(Integer[]::new);
Arrays.sort(tmp, (a, b) -> b - a);
Integer[] diff = new Integer[n/2];
for(int i = 0; i < n/2; i++){
answer += tmp[i*2+1];
diff[i] = tmp[i*2]-tmp[i*2+1];
}
Arrays.sort(diff,(a, b) -> b - a);
for(int i = 0; i < k; i++){
answer += diff[i];
}
return answer;
}
public static void main(String[] args){
Question3 T = new Question3();
System.out.println(T.solution(new int[]{7, 8, 5, 12, 3, 1, 3, 1, 1, 12}, 2));
System.out.println(T.solution(new int[]{8, 2, 12, 12, 12, 12, 2, 2}, 2));
System.out.println(T.solution(new int[]{3, 7, 12, 3, 3, 5, 7, 8, 9, 11, 23, 4, 6, 7}, 3));
System.out.println(T.solution(new int[]{12, 34, 56, 23, 22, 34, 55, 45, 24, 23, 45, 55, 55, 23, 11, 12, 23, 12}, 3));
System.out.println(T.solution(new int[]{14, 15, 20, 11, 10, 20, 20, 12, 9, 22, 27, 25, 30, 19}, 3));
}
}