Doubt in Matrix Search

The code is running fine.However only one test case is being passed.In total there are three test cases.
Here is the code given below

hey @ratulhans, replace you for loop with this
while(i<=(row-1) && j>=0)
{
if(arr[i][j]==o)
{
ans=true;
break;
}
else if(o<arr[i][j])
{
–j;
}
else
{
i++;
}
}

#include
using namespace std;
int main ()
{
int m,n,o,i,j,arr[100][100];
bool ans;
cin>>m>>n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>arr[i][j];
cin>>o;
i=0,j=n-1;
ans=false;
while(i<m&&j>=0)
{
if(o==arr[i][j])
ans=true;
else if(o<arr[i][j])
j–;
else if(o>arr[i][j])
i++;
}
cout<<ans;
}

I have made the necessary change but is unable to see the output.Kindly help.

hey @ratulhans, you should break when match is found.I have made changes to your code you can check them here.
https://ide.codingblocks.com/s/111123

Thanks. Now it’s working.

hey @ratulhans , if your query is resolved. Please mark this doubt as resolved and rate me on the basis of your experience.
rating option will appear when to mark this doubt as resolved