in editorial solution for this question there is a loop for row
but i don’ think that is required.
check this code.
#include
using namespace std;
void matrix_search(int **a,int m,int n,int key){
int row=0;
for(int col=n-1;col>=0 && row<m;){
if(a[row][col]==key){
cout<<“1”;
return;
}
if(a[row][col]>key){
col–;
}else{
row++;
}
}
cout<<“0”;
}
int main() {
int m,n;
cin>>m>>n;
int *a=new int[m];
for(int i=0;i<n;i++){
a[i]=new int [n];
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int key;
cin>>key;
matrix_search(a,m,n,key);
return 0;
}