-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathceiling_in_a_sorted_array.cpp
More file actions
40 lines (39 loc) · 928 Bytes
/
ceiling_in_a_sorted_array.cpp
File metadata and controls
40 lines (39 loc) · 928 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
#include <bits/stdc++.h>
using namespace std;
void search(){
int arr[]={1, 2, 8, 10, 10, 12, 19};
int ele=13, low=0, high=7, mid;
if(ele<arr[0]){
cout<<"floor= Non existent"<<endl;
cout<<"ceil= "<<arr[0]<<endl;
return;
}
else if(ele>arr[high-1]){
cout<<"floor= "<<arr[high-1]<<endl;
cout<<"ceil= Non existent"<<endl;
return;
}
else{
while(low<high && (high-low)>1){
mid=(low+high)/2;
if(ele==arr[mid]){
cout<<"floor= "<<ele<<endl;
cout<<"ceil= "<<ele<<endl;
return;
}
else if(arr[mid]<ele){
low=mid;
}
else{
high=mid;
}
}
cout<<"floor= "<<arr[low]<<endl;
cout<<"ceil= "<<arr[high]<<endl;
return;
}
}
int main(){
search();
return 0;
}