- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1471.java
35 lines (33 loc) · 1.21 KB
/
_1471.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;
importjava.util.TreeMap;
publicclass_1471 {
publicstaticclassSolution1 {
publicint[] getStrongest(int[] arr, intk) {
Arrays.sort(arr);
intmedian = arr.length % 2 != 0 ? arr[arr.length / 2] : arr[arr.length / 2 - 1];
TreeMap<Integer, List<Integer>> treeMap = newTreeMap<>(Collections.reverseOrder());
for (intnum : arr) {
intdiff = Math.abs(num - median);
if (!treeMap.containsKey(diff)) {
treeMap.put(diff, newArrayList<>());
}
treeMap.get(diff).add(num);
}
List<Integer> sorted = newArrayList<>();
for (intkey : treeMap.keySet()) {
List<Integer> sort = treeMap.get(key);
Collections.sort(sort, Collections.reverseOrder());
sorted.addAll(sort);
}
int[] result = newint[k];
for (inti = 0; i < k; i++) {
result[i] = sorted.get(i);
}
returnresult;
}
}
}