-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_assignment.cpp
More file actions
91 lines (88 loc) · 1.9 KB
/
binary_search_assignment.cpp
File metadata and controls
91 lines (88 loc) · 1.9 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
85
86
87
88
89
90
91
#include<bits/stdc++.h>
using namespace std;
int lowerbound(vector<int>v,int x){
int upper=v.size()-1, lower=0, mid=(upper+lower)/2;
while(upper>=lower){
if(upper==lower+1){
return v[upper];
}
else if(x==v[mid]){
return v[mid+1];
break;
}
else if(x>v[mid]){
lower=mid;
mid=(upper+lower)/2;
}
else if(x<v[mid]){
upper=mid;
mid=(upper+lower)/2;
}
}
}
int upperbound(vector<int>v,int x){
int upper=v.size()-1, lower=0, mid=(upper+lower)/2;
while(upper>=lower){
if(upper==lower+1){
return v[upper];
}
else if(x==v[mid]){
return v[mid+1];
break;
}
else if(x>v[mid]){
lower=mid;
mid=(upper+lower)/2;
}
else if(x<v[mid]){
upper=mid;
mid=(upper+lower)/2;
}
}
}
int ispresent(vector<int>v,int x){
int upper=v.size()-1, lower=0, mid=(upper+lower)/2;
while(upper>=lower){
if(upper==lower+1){
if(upper==x||lower==x){
return true;
}
else{
return false;
}
}
else if(x==v[mid]){
return true;
break;
}
else if(x>v[mid]){
lower=mid;
mid=(upper+lower)/2;
}
else if(x<v[mid]){
upper=mid;
mid=(upper+lower)/2;
} else{
return false;
}
}
}
int main(){
int n;
cin>>n;
vector<int>v(n);
for(int i=0 ; i<n ; i++){
cin>>v[i];
}
int x;
cin>>x;
sort(v.begin(),v.end());
cout<<lowerbound(v,x)<<endl;
cout<<upperbound(v,x)<<endl;
if(ispresent(v,x)){
cout<<"true"<<endl;
} else {
cout<<"false"<<endl;
}
return 0;
}