Current streak:
0 days
Longest streak:
4 days
Less
More
int firstOcc(vector<int>&arr,int n, int x){
int low=0, high=n-1;
int mid;
while (low <= high) {
mid=(low+high)/2;
if((mid==0 && arr[mid]==x) || (arr[mid]==x && arr[mid-1]<x)){
return mid;
}
else if(x>arr[mid]){
low=mid+1;
}
else{
high=mid-1;
}
}
return -1;
}
int lastOcc(vector<int>&arr, int n, int x){
int low=0, high=n-1;
int mid;
while(low<=high){
mid=(low+high)/2;
if((mid==n-1 && arr[mid]==x) || (arr[mid]==x && arr[mid+1]>x)){
return mid;
}
else if(x<arr[mid]){
high=mid-1;
}
else{
low=mid+1;
}
}
return -1;
}
pair<int, int> findFirstLastPosition(vector<int> &arr, int n, int x)
{
pair<int,int>pr;
pr.first=firstOcc(arr,n,x);
pr.second=lastOcc(arr,n,x);
return pr;
}