C++ O(log(n)) Solution

google
programming
interview-questions
Tags: #<Tag:0x00007f182fca0af0> #<Tag:0x00007f182fca09b0> #<Tag:0x00007f182fca0848>

#1
int bin(vector<int>A,int B,int start,int end)
{
if(start<end)
{
    int i=(start+end)/2;
    if(A[i]==B)
    {
        while(A[i]==B)
        i++;
        return i;
    }
    else if(A[i]>B && A[i-1]<=B)
    return i;
    else if(A[i]<B)
    return bin(A,B,i+1,end);
    else
    return bin(A,B,start,i-1);
}
return start;
}

int Solution::solve(vector<int> &A, int B) {
if(A.size()==0)
return 0;
return bin(A,B,0,A.size());
}