In matrix search problem i have used binary search. but one test case is failing

#include
using namespace std;

int bsearch(int b[],int num,int x)
{
int s=0;
int e=num-1;
while(s<=e)
{
int mid=(s+e)/2;

    if(x==b[mid])
        return 1;
    else if(x<b[mid])
      e=mid-1;
     else 
      s=mid+1;     

}
return 0;

}

int main() {
int n,m;
int a[30][30];
int b[1000];
cin>>n>>m;
int num=n*m;
int k=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>a[i][j];
b[k]=a[i][j];
k++;
}
int x;
cin>>x;
cout<<bsearch(b,num,x);

return 0;

}

@ankityadav943 Hey Ankit, If you are using binary Search in matrix search Problem then you have to apply binary Search on every row or column because your code is falling on below input.
Input
3 3
1 10 15
3 12 16
4 13 18
3

In this input linear array which you are using will not store the element in sorted order. you can check this by just printing the “b” array.

ur a maxtrix which is 2d is sorted along row and column but ur b maxtrix whic h is id is not sorted u ccant apply binay search in it
apply the search on 2 d matrix start with the last column of first row
if element id bigger move down and if smaller mover left

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.