[C++] Easy Binary Search solution

int ascBinarySearch(vector<int>&A,int left,int right,int B){
    while(left<=right){
        int mid=left+(right-left)/2;
        if(A[mid]==B){
            return mid;
        }
        else if(A[mid]<B){
            left=mid+1;
        }
        else{
            right=mid-1;
        }
    }
    return -1;
}
int dscBinarySearch(vector<int>&A,int left,int right,int B){
    while(left<=right){
        int mid=left+(right-left)/2;
        if(A[mid]==B){
            return mid;
        }
        else if(A[mid]<B){
            right=mid-1;
        }
        else{
            left=mid+1;
        }
    }
    return -1;
}
int findPeakIndex(vector<int> &A){
    int n=A.size();
    int left=0,right=n-1;
    while(left<=right){
        int mid=left+(right-left)/2;
        int toLeft= mid==0 ? INT_MIN : A[mid-1];
        int toRight= mid==n-1 ? INT_MIN : A[mid+1];
        if(A[mid]>toLeft && A[mid]>toRight){
            return mid;
        }
        else if(A[mid]>toLeft){
            left=mid+1;
        }
        else{
            right=mid-1;
        }
    }
    return -1;
}
int Solution::solve(vector<int> &A, int B) {
    int n=A.size();
    int index=findPeakIndex(A);
    int x=ascBinarySearch(A,0,index,B);
    int y=dscBinarySearch(A,index+1,n-1,B);
    if(x==-1){
        return y;
    }
    return x;
}
Click here to start solving coding interview questions