-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloorOfNumber.java
More file actions
34 lines (28 loc) · 848 Bytes
/
floorOfNumber.java
File metadata and controls
34 lines (28 loc) · 848 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
public class floorOfNumber {
public static void main(String[] args) {
int[] arr ={12,15,17,23,26,29,34,37};
int ans = floorOfNumbers(arr, 10);
System.out.println(ans);
}
static int floorOfNumbers(int[] arr, int target){
int start = 0;
int end = arr.length - 1;
if(target<arr[start]){
return -1;
}
while (start <= end) {
// int middle = (start + end) / 2;
int middle = start + (end - start)/2;
if (arr[middle] == target) {
return middle;
}
else if (target > arr[middle]) {
start = middle + 1;
}
else {
end = middle - 1;
}
}
return arr[end];
}
}