-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber of occurrence.cpp
47 lines (41 loc) · 1.17 KB
/
Number of occurrence.cpp
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
class Solution {
public:
int findIndex(vector<int>& arr, int target, int start, int end){
while(start <= end){
int mid = start+(end-start)/2;
if(arr[mid] == target) return mid;
else if(arr[mid] > target) end = mid-1;
else start = mid+1;
}
return -1;
}
int countFreq(vector<int>& arr, int target) {
// code here
int i=-1, j=-1, n=arr.size(), st=0, end=n-1;
// Find least index
while(i != 0){
int temp = findIndex(arr, target, st, end);
if(temp != -1){
i = temp;
end = i-1;
} else{
break;
}
}
// Find Highest index
st = 0, end = n-1;
while(j != n-1){
int temp = findIndex(arr, target, st, end);
if(temp != -1){
j = temp;
st = j+1;
} else{
break;
}
}
// cout<<"(i,j): "<<i<<" "<<j<<"\n";
// If element doesn't exists, return 0
if(i==-1 && j==-1) return 0;
return (j-i+1);
}
};