-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfirst_last_occurence_of_element.cpp
69 lines (57 loc) · 1.33 KB
/
first_last_occurence_of_element.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;
int firstOc(int arr[], int size , int t){
int s = 0;
int e = size-1;
int ans = -1;
while(s<=e){
int mid = s + (e-s)/2;
if(arr[mid] == t){
ans = mid;
e = mid - 1;
}
else if(arr[mid] < t){
s = mid + 1;
}
else if(arr[mid] > t){
e = mid - 1;
}
}
return ans;
}
int lastOc(int arr[], int size , int t){
int s = 0;
int e = size - 1;
int ans = -1;
while(s<=e){
int mid = (s+e)/2;
if(arr[mid]== t){
ans = mid;
s = mid+1;
}
else if(arr[mid] > t){
e = mid - 1;
}else{
s = mid + 1;
}
}
return ans;
}
int main() {
int n;
cout<<"Enter number of elements in array : ";
cin>>n;
int arr[n];
cout<<"Enter elements of array in increasing order "<<endl;
for(int i = 0; i< n; i++) {
cin>>arr[i];
}
int t;
cout<<"enter element whose first and last index you want : ";
cin>>t;
int F_O = firstOc(arr, n, t);
int L_O = lastOc(arr, n, t);
cout<<"First index of "<<t<<" is : "<<F_O<<endl;
cout<<"Last index of "<<t<<"4 is : "<<L_O<<endl;
cout<<"Total nuber of occurence of "<<t<< " is : "<<L_O -F_O +1;
}