forked from Sinhov/Algorithms_Example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlower_bound.cpp
More file actions
48 lines (40 loc) · 885 Bytes
/
Copy pathlower_bound.cpp
File metadata and controls
48 lines (40 loc) · 885 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
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;
//Modified Binary Search to find the lower bound of a key if present in the array
//Lower bound is the index of the first occurrence of a given key
// if a[]={1,2,2,3,4,5,5,6}
// lower_bound for key 2 is 1
// lower_bound for key 5 is 5
int lower_bound(int low,int high,int key)
{
int result=-1;
// result is to keep track of the previous index found if any
while(low<=high)
{
int mid=(low + high)/2;
if(a[mid]==key)
{
result=mid;
high=mid-1;
}
else if(a[mid]< key)
{
low=mid +1;
}
else if(a[mid]>key)
{
high=mid-1;
}
}
return result;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
int x;
cin>>x;
cout<<lower_bound(0,n-1,x);
}