Test case failing in Binary search implementation


#1

while implementing Binary search algo for finding closest element when i used while(low<high) it was failing the test case but when i put while(low<-high) it passes all the test cases. Can anybody help me why was this happening?
here is the implementation of BS

int BS(const vector<int> &A, int l, int h, int tar){
    int mid;
    if(tar<=A[0])
        return A[0];
    if(tar>=A[h])
        return A[h];
    while(l<=h){ // while(l<h) fails the test cases
        mid=l+(h-l)/2;
        if(A[mid]==tar)
            return A[mid];
        else if(A[mid]>tar)
            h=mid-1;
        else l=mid+1;
    }
    if(tar>A[mid])
        return abs(A[mid]-tar)>abs(A[mid+1]-tar)?A[mid+1]:A[mid];
    else return abs(A[mid]-tar)>abs(A[mid-1]-tar)?A[mid-1]:A[mid];
}