Log(m*n) method, What is wrong with my code ? It doesnt work on one column matrix but it works on GFG for that same test case


#1

Comment body goes here.
Test Case: 6 1
17 27 28 31 58 62
95

My code is below:

int n=A.size();
int m=A[0].size();
int low=0;
int high=n;
int mid;
int target_row=-1;
while(low<=high)
{
    mid=low+(high-low)/2;
    if(A[mid][m-1]>=B && A[mid][0]<=B)
    {
        target_row=mid;
        break;
    }
    else if(A[mid][m-1]>B && A[mid][0]>B)
    {
        high=mid-1;
    }
    else if(A[mid][m-1]<B && A[mid][0]<B)
    {
        low=mid+1;
    }
}

if(target_row==-1)
return 0;

////finding element in the target_row

low=0;
high=m;
while(low<=high)
{
    mid=low+(high-low)/2;
    if(A[target_row][mid]==B)
    {
        return 1;
    }
    else if(A[target_row][mid]<B)
    {
        low=mid+1;
    }
    else if(A[target_row][mid]>B)
    {
        high=mid-1;
    }
}
return 0;