Matrix search problem

this code is not passing only one test case
#include
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
int a[300][300];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
int key;
cin>>key;
int i=0;
int j=n-1;
while(i<m&&j>=0)
{
if(key<a[i][j])
{
j–;
}
if(key>a[i][j])
{
i++;
}
if(key==a[i][j])
{
cout<<“1”;
break;
}
else
{
cout<<“0”;
break;
}

}

}

remove this!
instead use boolean flag and set it to 1 whenever you find the element!
after coming out of while loop, if flag is set then element was already found so do nothing, else print 0

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.