forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupper_bound
More file actions
48 lines (41 loc) · 887 Bytes
/
Copy pathupper_bound
File metadata and controls
48 lines (41 loc) · 887 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 upper bound of a key if present in the array
//Upper bound is the index of the last occurrence of a given key
// if a[]={1,2,2,3,4,5,5,6}
// upper_bound for key 2 is 2
// upper_bound for key 5 is 6
int a[100];
int upper_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;
low=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<<upper_bound(0,n-1,x);
}