-
Notifications
You must be signed in to change notification settings - Fork 0
/
arraybinaryfirstandlast.cpp
55 lines (50 loc) · 1.03 KB
/
arraybinaryfirstandlast.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
// input = 0 1 1 2 2 2 2 2
// output = 3 7
/*#include<iostream>
using namespace std ;
int firstandlast(int array[] , int n , int k){
for(int i = 0 ; i<n ; i++)
{
if(array[i]==k)
{
cout<<"your element first occurence at positon"<<i<<endl;
break;
}
}
for(int j = n-1 ; j<n ; j--)
{
if(array[j]==k)
{
cout<<"your element last occurence at positon"<<j<<endl;
break;
}
}
}
int main()
{
int a[6] = {0,1,2,15,2,13};
firstandlast(a , 6 , 2);
}*/
#include<iostream>
using namespace std ;
int firstandlast(int array[] , int n , int k)
{
int start = 0;
int end = n - 1 ;
int mid = start + (end - start)/2;
if(array[mid]==k)
{
for(int i = start ; i<mid ; i++)
{
if(array[start]==k)
{
cout<<start;
}
}
}
}
int main()
{
int a[6] = {0,2,2,2,4,5};
firstandlast(a,6,2);
}