Search for a Range **Recursive approach solution**


#1

int binary1(const vector &v,int ele,int left,int right,int finl)
{
if(left<=right)
{
int mid=left+(right-left)/2;

if(ele==v[mid])
{
    finl=mid;
    return binary1(v,ele,left,mid-1,finl);
}
else if(ele<v[mid])
{
    return binary1(v,ele,left,mid-1,finl);
}
else
{
    return binary1(v,ele,mid+1,right,finl);
}
}


return finl;

}

int binary2(const vector &v,int ele,int left,int right,int finl)
{
if(left<=right)
{
int mid=left+(right-left)/2;

if(ele==v[mid])
{
    finl=mid;
    return binary2(v,ele,mid+1,right,finl);
}
else if(ele>v[mid])
{
            return binary2(v,ele,mid+1,right,finl);
}
else
{
    return binary2(v,ele,left,mid-1,finl);
}
}


return finl;

}
vector Solution::searchRange(const vector &A, int B) {

int l=binary1(A,B,0,A.size()-1,-1);

int r=binary2(A,B,0,A.size()-1,-1);

vector<int>v;
v.push_back(l);
v.push_back(r);
return v;

}