-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloorCeil.java
More file actions
43 lines (25 loc) · 780 Bytes
/
Copy pathFloorCeil.java
File metadata and controls
43 lines (25 loc) · 780 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
40
41
42
43
package Prep;
public class FloorCeil {
// binary search approach
// other
public static void helper(int arr[], int x) {
int fInd = -1, cInd = -1;
int n = arr.length;
int fDist = Integer.MAX_VALUE, cDist = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (arr[i] >= x && cDist > (arr[i] - x)) {
cInd = i;
cDist = arr[i] - x;
}
if (arr[i] <= x && fDist > (x - arr[i])) {
fInd = i;
fDist = x - arr[i];
}
}
System.out.println("floor" + arr[fInd] + " ceil" + arr[cInd]);
}
public static void main(String[] args) {
int arr[] = {1, 4, 6, 8, 9};
helper(arr, 3);
}
}