-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstAndLastOccurance.cpp
48 lines (44 loc) · 1002 Bytes
/
firstAndLastOccurance.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
#include<bits/stdc++.h>
using namespace std;
int binarySearch(int arr[],int target,bool findStartIndex)
{
int ans=-1;
int low=0;
int high=sizeof(arr)/sizeof(arr[0])-1;
while(low<=high)
{
int mid=low+(high-low)/2;
if(arr[mid]<target)
{
low=mid+1;
}
else if(arr[mid]>target)
{
high=mid-1;
}
else // potential answer is found
{
ans=mid; // potential answer
if(findStartIndex)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
}
return ans;
}
int main()
{
int answer[2]={-1,-1};
int arr[]={5,7,7,7,7,8,8,10};
int target=8;
bool findStartIndex;
answer[0]=binarySearch(arr,target,findStartIndex=true);
answer[1]=binarySearch(arr,target,findStartIndex=false);
cout<<"["<<answer[0]<<","<<answer[1]<<"]";
return 0;
}