-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImplementing_Upper_bound.cpp
More file actions
39 lines (35 loc) · 962 Bytes
/
Copy pathImplementing_Upper_bound.cpp
File metadata and controls
39 lines (35 loc) · 962 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
#include<bits/stdc++.h>
using namespace std;
int BS(int n, vector<int> &v, int target, int low, int high, int ub){
if(low > high){
return ub;
}
int mid = low + (high-low)/2;
if(v[mid] <= target){
//ub = mid;
return BS(n, v, target, mid+1, high, ub);
}else{
ub = v[mid];
return BS(n, v, target, low, mid-1, ub);
}
return -1;
}
int main(){
cout<<"Enter the size of Sorted array : \n";
int n; cin>>n;
cout<<"Enter the elements of Sorted array : \n";
vector<int> v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
//int low=0, high=n-1, lb =n;
cout<<"No. of testcases: \n";
int testcases; cin>>testcases;
while(testcases--){
cout<<"Enter the target element : \n";
int target; cin>>target;
cout<<"The upper_bound of "<<target<<" is: "<<BS(n, v, target, 0, n-1, -1);
cout<<"\n";
}
return 0;
}