forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKClosestElement.java
35 lines (29 loc) · 943 Bytes
/
KClosestElement.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
import java.util.ArrayList;
import java.util.List;
public class KClosestElement {
public static void main(String[] args) {
int[] arr = {1,1,2,2,2,2,2,3,3};
System.out.println( findClosestElements(arr, 3, 3) );
}
static List<Integer> findClosestElements(int[] arr, int k, int x) {
int s = 0;
int e = arr.length-k;
while(s < e){
int m = s + (e-s)/2;
int indexJustAfterWindow = m+k;
while(indexJustAfterWindow <= e && arr[m] == arr[indexJustAfterWindow] ){
indexJustAfterWindow++;
}
if( Math.abs( arr[m] - x ) > Math.abs( x - arr[indexJustAfterWindow] ) ){
s = m+1;
} else{
e = m;
}
}
List<Integer> list = new ArrayList<>();
for(int i = s; i < s +k ; i++){
list.add( arr[i] );
}
return list;
}
}