forked from Itachi-Ucchiha/BasicDSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3June(III),java
More file actions
34 lines (29 loc) · 870 Bytes
/
Copy path3June(III),java
File metadata and controls
34 lines (29 loc) · 870 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 NearestLowestAndGreatest {
public static void main(String[] args) {
int[] arr = {10,20,30,40,50,60,70,80};
int search =72;
//TC = O(n)
int low =0;
int mid = 0;
int nearestLowest = 0;
int nearestGreatest = 0;
int high = arr.length -1 ;
for(int i=0;i<arr.length; i++){
mid = (low+high)/2;
if(search>arr[mid]){
low = mid+1;
nearestLowest=arr[mid];
}
else if(search<arr[mid]){
high=mid-1;
nearestGreatest=arr[mid];
}
else{
nearestGreatest = nearestLowest = arr[mid];
break;
}
}
System.out.println(nearestGreatest);
System.out.println(nearestLowest);
}
}