- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathfirst_occur.cpp
34 lines (31 loc) · 687 Bytes
/
first_occur.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
#include<bits/stdc++.h>
usingnamespacestd;
#definelllonglong
#definepb push_back
#definemod1000000007
#defineendl'\n'
voidfindFirstAndLast(int arr[], int n, int x)
{
int first = -1, last = -1;
for (int i = 0; i < n; i++) {
if (x != arr[i])
continue;
if (first == -1)
first = i;
last = i;
}
if (first != -1)
cout << "First Occurrence = " << first
<< "\nLast Occurrence = " << last;
else
cout << "Not Found";
}
intmain(){
cin.tie(0)->sync_with_stdio(0);
int arr[]={1,2,4,4,4,6};
int n=sizeof(arr)/sizeof(int);
int key;
key=4;
findFirstAndLast(arr,n,key);
return0;
}