Skip to content

Instantly share code, notes, and snippets.

@mishal23
Last active May 15, 2018 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishal23/f2682b37f1ef3eb9daf027419cd19275 to your computer and use it in GitHub Desktop.
Save mishal23/f2682b37f1ef3eb9daf027419cd19275 to your computer and use it in GitHub Desktop.
#include "bits/stdc++.h"
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m,x,i,j;
cin>>n>>m;
int a[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>a[i][j];
}
}
cin>>x;
i=0;
j=m-1;
bool done=false;
while(i<n && j>=0)
{
if(a[i][j]==x)
{
done=true;
cout<<"1"<<endl;
break;
}
else if(a[i][j]<x)
{
i++;
j=m-1;
}
else
{
j--;
}
}
if(!done)
{
cout<<"0"<<endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment