C++ solution O(logn)


#1

vector Solution::searchRange(const vector &A, int B) {
vectorans(2,-1);
int n=A.size();
int l=0,h=n-1;
while(l<=h)
{
int m=l+(h-l)/2;
if(A[m]==B)
{
while(m-1>=l &&A[m-1]==B) m–;
ans[0]=m;
while(m+1<=h &&A[m+1]==B) m++;
ans[1]=m;
}
if(A[m]>B) h=m-1;
else l=m+1;
}
return ans;
}