Help plzz.. getting partial marks, but solved in log(n) time


#1

#define ll long long
int Solution::search(const vector &A, int B) {
ll int lo=0, hi=A.size()-1;
ll int mid=(lo+hi)/2;
while(A[lo]>A[mid] || A[mid]>A[hi])
{

 if(A[lo]<A[mid])
    {
        if(A[lo]<=B && A[mid]>=B)
        {
            hi=mid;
        }
        else
        {
            lo=mid+1;
        }
    }
    else
    {
        if(A[mid]<=B && A[hi]>=B)
        {
            lo=mid+1;
        }
        else
        {
            hi=mid;
        }
    }
    mid=(lo+hi)/2;
}
while(lo<hi)
{
    mid=(lo+hi)/2;
    if(A[mid]>=B)
    {
        hi=mid;
    }
    else
    {
        lo=mid+1;
    }
}
if(A[lo]==B)
{
    return lo;
}
else
{
    return -1;
}

}