-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathCountOccurancesUsingBinarySearch.cpp
More file actions
65 lines (60 loc) · 1.02 KB
/
CountOccurancesUsingBinarySearch.cpp
File metadata and controls
65 lines (60 loc) · 1.02 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
// Program to count occurances of a number in Sorted Array Using binary Search
#include<iostream>
#include<cstdio>
using namespace std;
int a[1000000];
int count = 0;
int getLeft(int low,int high,int key){
int mid ,ans = -1;
while(low<=high){
mid = low + (high-low)/2;
if(a[mid]>key)
high = mid - 1;
else if(a[mid]<key )
low = mid + 1;
else{
ans = mid;
high = mid -1;
}
}
return ans;
}
int getRight(int low,int high,int key){
int mid , ans = -1;
while(low<=high){
mid = low + (high-low)/2;
if(a[mid]>key){
high = mid-1;
}
else if(a[mid]<key){
low = mid + 1;
}
else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
void countOccurances(int n,int key){
int x = getLeft(0,n-1,key);
int y = getRight(0,n-1,key);
cout<<"Left Position :"<<x<<endl;
cout<<"Right Position :"<<y<<endl;
if(x!=-1&&y!=-1&&a[x]==key&&a[y]==key)
cout<<(y-x+1)<<endl;
else
cout<<"Not found !"<<endl;
}
int main(){
int t,n,i,key;
cin>>t;
while(t--){
cin>>n;
for(i=0;i<n;i++) scanf("%d",&a[i]);
count=0;
cin>>key;
countOccurances(n,key);
}
return 0;
}