Binary search using bool parameter


#1

int binary(const vector &A,int x,bool y)//value of bool determines first and last occurence
{
int mid,low=0,high=A.size()-1,index=-1;
while(low<=high)
{ mid=(low+high)/2;
if(A[mid]==x)
{
index=mid;
if(y)//if bool is true it will find in the first half (first occurence)
high=mid-1;
else//for last occurence
low=mid+1;
}
else if(A[mid]<x)
low=mid+1;
else
high=mid-1;
}
return index;
}
vector Solution::searchRange(const vector &A, int B) {
vectorX;
int a=binary(A,B,true),b=binary(A,B,false);
X.push_back(a);
X.push_back(b);
return X;

}