-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path030.TotalNo.Occurance.cpp
50 lines (49 loc) · 1.2 KB
/
030.TotalNo.Occurance.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
48
49
50
#include<iostream>
using namespace std;
int firstocc(int arr[], int size, int n) {
int s = 0, e = size - 1, first = -1;
while (s <= e) {
int mid = s + (e - s) / 2;
if (arr[mid] == n) {
first = mid;
e = mid - 1;
} else if (arr[mid] < n) {
s = mid + 1;
} else {
e = mid - 1;
}
}
return first;
}
int lastocc(int arr[], int size, int n) {
int s = 0, e = size - 1, second = -1;
while (s <= e) {
int mid = s + (e - s) / 2;
if (arr[mid] == n) {
second = mid;
s = mid + 1;
} else if (arr[mid] < n) {
s = mid + 1;
} else {
e = mid - 1;
}
}
return second;
}
int main()
{
int n,arr[100],size,first,second,ans;
cout << "enter the size of the array:" << endl;
cin >> size;
cout << "enter the array:" << endl;
for(int i=0; i<size; i++)
{
cin >> arr[i];
}
cout << "enter the number to find no. of total occurance:" << endl;
cin >> n;
first = firstocc(arr,size,n);
second = lastocc(arr,size,n);
ans = (second - first) + 1;
cout << "Total number of occurance:" << ans << endl;
}