-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalgorithm2.cpp
More file actions
74 lines (61 loc) · 1.6 KB
/
algorithm2.cpp
File metadata and controls
74 lines (61 loc) · 1.6 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
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int n,a;
vector<int> v;
v.push_back(5);
v.push_back(7);
v.push_back(7);
v.push_back(8);
v.push_back(8);
v.push_back(10);
sort(v.begin(),v.end());
cout<<"Index: \n";
for(int i=0;i<6;i++)
{
cout<<i<<" ";
}
cout<<endl;
for(int i:v)
{
cout<<i<<" ";
}
// auto it=lower_bound(v.begin(),v.end(),2);
// auto it2=upper_bound(v.begin(),v.end(),2);
// auto it=lower_bound(v.begin(),v.end(),3);
// auto it2=upper_bound(v.begin(),v.end(),3);
// auto it=lower_bound(v.begin(),v.end(),4);
// auto it2=upper_bound(v.begin(),v.end(),4);
auto it=lower_bound(v.begin(),v.end(),2);
auto it2=upper_bound(v.begin(),v.end(),2); //this is how we get element
if(it == v.end())
{
cout<<endl<<"not found";
}
else
{
cout<<endl<<*it<<endl;
}
if(it2==v.end())
{
cout<<endl<<"not found";
}
else
{
cout<<*it2;
}
cout<<endl<<"Index of lower bound of 2: "<<lower_bound(v.begin(),v.end(),2)-v.begin();
cout<<endl<<"Index of upper bound of 2: "<<upper_bound(v.begin(),v.end(),2)-v.begin()-1;
//this is how we get that array bound index be it LB or UB
vector<int> k;
k.push_back(lower_bound(v.begin(),v.end(),2)-v.begin());
k.push_back(upper_bound(v.begin(),v.end(),2)-v.begin()-1);
for(int i:k)
{
cout<<i<<" ";
}
cout<<binary_search(v.begin(),v.end(),8);
}