Cpp soln using binary search


#1

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

int n = A.size();
int i , l=0,h=n-1 , ans = 0;

while(l<=h)
{
    int m = l+(h-l)/2;
    if(A[m] > B) h = m-1;
    else if(A[m] < B) l = m+1;
    else{
        i = m;
        break;
    }
}
int x = i+1;
while(A[i] == B&&i>=0){
    ans++;
    i--;
}
while(A[x] == B && x<n){
    ans++;
    x++;
}

return ans;

}


#2

Isn’t the complexity of this code O(logn)+O(m), m being the number count for B in A?