Given a matrix with row sorted and column sorted, find the number given:
static boolean searchGivenNo(int[][] arr, int x){
int rows = arr.length;
if(rows == 0) return false;
int cols = arr[0].length;
for(int i = 0; i<rows; i++)
{
for(int j = cols-1; j>=0; j--)
{
if(arr[i][j] == x) return true;
if(arr[i][j] < x) break;
}
}
return false;
}
References:
http://www.geeksforgeeks.org/search-in-row-wise-and-column-wise-sorted-matrix/
No comments:
Post a Comment