Simple short O(n+m)complexity code


#1

While considering the top right element of the matrix,
we will compare it with our target value
If target is less then reduce the column else row.

SOURCE CODE;-

int Solution::solve(vector<vector > &A, int B) {
int r=A.size();
int c=A[0].size();
int i=0,j=c-1;
while(i<r && j>=0){
if(A[i][j]==B)
return ((i+1)*1009 + (j+1));
if(A[i][j]>B)
j–;
else
i++;
}
return -1;
}