-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (30 loc) · 978 Bytes
/
Solution.java
File metadata and controls
39 lines (30 loc) · 978 Bytes
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
/**
@lc id : 1471
@problem : The k Strongest Values in an Array
@author : github.com/rohitkumar-rk
@url : https://leetcode.com/problems/the-k-strongest-values-in-an-array/
@difficulty : medium
*/
class Solution {
public int[] getStrongest(int[] arr, int k) {
int[] temp = arr;
Arrays.sort(temp);
int n = arr.length;
int m = temp[(n-1)/2];
Integer[] boxed = new Integer[arr.length];
for(int i = 0; i < arr.length; i++)
boxed[i] = arr[i];
Arrays.sort(boxed, new Comparator<Integer>(){
public int compare(Integer a, Integer b){
if(Math.abs(a-m) == Math.abs(b-m))
return a-b;
else return Math.abs(a-m) - Math.abs(b-m);
}
});
int[] res = new int[k];
for(int i=0;i<k;i++){
res[i] = boxed[n-i-1];
}
return res;
}
}