-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindKClosestElements.java
37 lines (31 loc) · 990 Bytes
/
FindKClosestElements.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
36
37
package com.company;
import java.util.ArrayList;
import java.util.List;
//https://leetcode.com/problems/find-k-closest-elements/?envType=study-plan&id=binary-search-ii
public class FindKClosestElements {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
int k = 4;
int x = 3;
int y = -1;
System.out.println(findClosestElements(arr,k,x));
System.out.println(findClosestElements(arr, k, y));
}
static List<Integer> findClosestElements(int[] arr, int k, int x){
int start = 0;
int end = arr.length - k;
while(start < end){
int mid = start + (end - start)/2;
if( x - arr[mid] > arr[mid + k] - x){
start = mid + 1;
} else{
end = mid;
}
}
List<Integer> res = new ArrayList<>();
for(int i = start; i < start + k; i++){
res.add(arr[i]);
}
return res;
}
}