Search in a row-column sorted Matrix | Practice | GeeksforGeeks

Given a matrix of size n x m, where every row and column is sorted in increasing order, and a number x. Find whether element x is present in the matrix or not.

Example 1:
Input:
n = 3, m = 3, x = 62
matrix[][] = {{ 3, 30, 38},
              {36, 43, 60},
              {40, 51, 69}}
Output: 0
Explanation:
62 is not present in the matrix, 
so output is 0.

Example 2:
Input:
n = 1, m = 6, x = 55
matrix[][] = {{18, 21, 27, 38, 55, 67}}
Output: 1
Explanation: 55 is present in the matrix.


Your Task:
You don't need to read input or print anything. Complete the function search() that takes n, m, x, and matrix[][] as input parameters and returns a boolean value. True if x is present in the matrix and false if it is not present.

Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N, M <= 1000
1 <= mat[][] <= 105
1 <= X <= 1000
+1
velspace015 days ago
Java sol
int row=0;
	    int col=m-1;
	    while(row<n && col>=0){
	        if(Mat[row][col]==x){
	            return true;
	        }
	        else if(Mat[row][col]>x){
	            col--;
	        }else{
	            row++;
	        }
	    }
	    return false;
+1
subhaduleygba1 week ago
//c++ code
bool search(vector<vector<int> > matrix, int n, int m, int x)    {       // code here        if(n==0)return false;              int i=n-1;       int j=0;       while(i>=0 and j<n){           if(matrix[j][i]==x){               return true;           }           else if(matrix[j][i]<x){               j++;           }else{               i--;           }       }       return false;   }
 
 
+2
vishalsavade2 weeks ago
Approach
1. Start with row 0 and col m-1
2. traverse while row < n and col >= 0
3. if element > target decrement col
4. else increment row
5. if found return true else false
-1
giridharsonu1232 weeks ago
JavaScript Solution
for (let i = 0; i < n; i++) {        for (let j = 0; j < m; j++) {            if (matrix[i][j] === x) {                return 1;            }        }    }    return 0;
0
dileeprajoriya20192 weeks ago
Problem Solved Successfully 
 
bool search(vector<vector<int> > matrix, int n, int m, int target)    {      int r = 0;       int c = m-1;              while( r < n and c >= 0)       {           int ele = matrix[r][c];           if(ele == target)           {               return true;           }           else if(ele > target)           {               c--;           } else {               r++;           }       }              return false;   }
0
rishaw2k2 weeks ago
time complexity 0.29/1.6
Simple and efficient approach in C++
     bool search(vector<vector<int> > matrix, int n, int m, int x) 
    {
        bool res = false; 
        int i=0, j=m-1;
        while(i<n && j>=0)
        {
            if(matrix[i][j] == x)
            {
                res = true;
                break;
            }
            else if(matrix[i][j] > x)
                j--;
            else 
                i++;
        }
        return res;
        // code here 
    }
+1
yashvardhansingh20193 weeks ago
Java
# Simple Binary Search
int i =0;    int j = m-1;    while(i<n && j>=0){        if(matrix[i][j]==x){            return true;        }else if(matrix[i][j]>x){            j--;    }else{        i++;    }}return false;
+1
mukulgoswami304
This comment was deleted.
0
sudeepgarg6711 month ago
class Solution:      #Function to search a given number in row-column sorted matrix.   def search(self,matrix, n, m, x):     for i in range(n):        if x in matrix[i]:            return 1    return 0
0
prakashsuraj41411 month ago
 Problem Solved Successfully 
bool search(vector<vector<int> > matrix, int n, int m, int x)    {       // code here        int i=0;       int j=m-1;       while(i>=0 && i<n && j>=0 && j<m)       {           if(matrix[i][j]==x)           return true;           else if(matrix[i][j]>x)           j--;           else           i++;       }       return false;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.