-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFindClosestElements.java
More file actions
84 lines (68 loc) · 2.33 KB
/
Copy pathFindClosestElements.java
File metadata and controls
84 lines (68 loc) · 2.33 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class FindClosestElements {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
int left = 0, right = arr.length - 1;
/*
* we find the index of element in the array if exist
* or one position before that
*/
int mid = 0, index = -1;
while (left <= right) {
mid = left + (right - left) / 2;
if (arr[mid] == x) {
index = mid;
break;
} else if (arr[mid] > x) {
right = mid -1;
} else {
left = mid + 1;
}
}
List<Integer> outputClosestElement = new ArrayList<>();
int rem = k;
/*
* if x doesn't exist in the array
*/
if (index == -1) {
// if element is not in the array
if (x < arr[0] || x > arr[arr.length - 1]) {
left = -1;
}
left = right;
right = left + 1;
} else { //x exist in the array
//System.out.println("[debug] x exist in the array");
outputClosestElement.add(arr[mid]);
left = mid - 1;
right= mid + 1;
rem--;
}
while (left >= 0 && right <= arr.length - 1 && rem > 0) {
int left_value = x - arr[left];
int right_value = arr[right] - x;
if (left_value <= right_value) {
//System.out.println(arr[left]);
outputClosestElement.add(arr[left]);
left--;
rem--;
}
else {
outputClosestElement.add(arr[right]);
right++;
rem--;
}
}
while (left >= 0 && rem > 0 ) {
outputClosestElement.add(arr[left--]);
rem--;
}
while (right < arr.length && rem > 0 ) {
outputClosestElement.add(arr[right++]);
rem--;
}
Collections.sort(outputClosestElement);
return outputClosestElement;
}
public static void main(String[] args) {
System.out.println(findClosestElements(new int[]{1, 3}, 1, 2));
}
}