Simple Binary Search Approach but submission not accepted


#1

int index(const vector &v, int x, bool any)
{
int l=0, r=v.size()-1;
int result=-1;
while(l<=r)
{
int mid=l+(r-l)/2;
if(v[mid]==x)
{
result=mid;
if(any)
r=mid-1;
else
l=mid+1;
}
else if(v[mid<x])
l=mid+1;
else
r=mid-1;
}
return result;
}
vector Solution::searchRange(const vector &A, int B) {
vector v;
int l=index(A,B,true);
int r=index(A,B,false);
v.push_back(l);
v.push_back®;
return v;
}