Search in a matrix | Practice | GeeksforGeeks

Given a matrix mat[][] of size N x M, where every row and column is sorted in increasing order, and a number X is given. The task is to find whether element X is present in the matrix or not.

Example 1:
Input:
N = 3, M = 3
mat[][] = 3 30 38 
         44 52 54 
         57 60 69
X = 62
Output:
0
Explanation:
62 is not present in the
matrix, so output is 0

Example 2:
Input:
N = 1, M = 6
mat[][] = 18 21 27 38 55 67
X = 55
Output:
1
Explanation:
55 is present in the
matrix at 5th cell.

Your Task:
You don't need to read input or print anything. You just have to complete the function matSearch() which takes a 2D matrix mat[][], its dimensions N and M and integer X as inputs and returns 1 if the element X is present in the matrix and 0 otherwise.

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

Constraints:
1 <= N, M <= 1005
1 <= mat[][] <= 10000000
1<= X <= 10000000
0
mayank180919995 hours ago
	int matSearch (vector <vector <int>> &mat, int N, int M, int X)
	{
	    // your code here
	    for(int i=0;i<N;i++){
	        for(int j=0;j<M;j++){
	            if(mat[i][j]==X){
	                return 1;
	            }
	        }
	    }
	   return 0;
	}
0
tusharbijalwan126 hours ago
class Solution{public:    int binary_Srch(int s,vector<int>ar,int low,int high){       low=0,high=ar.size()-1;       while(low<=high){           int mid=(low+high)/2;           if(ar[mid]==s){               return 1;           }           else if(ar[mid]>s){               high=mid-1;           }           else{               low=mid+1;           }       }       return 0;   }
int matSearch (vector <vector <int>> &mat, int N, int M, int x){    // your code here    vector<int>v;    for(int i=0;i<mat.size();i++){        for(int j=0;j<mat[0].size();j++){            v.push_back(mat[i][j]);        }    }    sort(v.begin(),v.end());    if(binary_Srch(x,v,0,v.size())){        return 1;    }    else{        return 0;    }    }};
 
+1
bhardwajji1 week ago
binary search row and col vise
int matSearch (int n, int m, int mat[][m], int x)
{
    
    int i=0 , j=m-1;
    while(i>=0 && j>=0 && i<n && j<m){
        if(mat[i][j] == x )
            return 1;
        else if(mat[i][j] < x){
            i++;
        }
        else{
            j--;
        }
        
    }
    return 0;
}
0
anishmeena0011 week ago
int matSearch (vector <vector <int>> &mat, int N, int M, int X){       // your code here    for(int i=0;i<N;i++)    {        for(int j=0;j<M;j++)        {            if(mat[i][j]==X)            return 1;        }    }return 0;}
0
scientiest311 week ago
def matSearch(self,mat, N, M, X): for i in range(N):     if mat[i][0] <= X and mat[i][M-1] >= X:         if X in mat[i]:             return 1          return 0
0
tusharbijalwan123 weeks ago
 int matSearch (vector <vector <int>> &mat, int N, int M, int X){    // your code here    int s=0;    for(int i=0;i<N;i++){        for(int j=0;j<M;j++){            if(mat[i][j]==X){                s=1;                }        }    }    return s;}
0
gunateja pm1 month ago
class Sol{   public static int matSearch(int mat[][], int N, int M, int X)   {       // your code here       int k =0;       for(int i[] : mat) {           for(int j : i) {               if(j==X) {                   k=1;               }               else if(j>X) {                   break;               }           }       }       return k;   }}
+3
17rashishukla2 months ago
//C++ solution
UPVOTE IT IF YOU LIKE AND UNDERSTAND THE SOLUTION
//It is basically startting from row =3 and col=0 
//If you have any doubt you can ask.
class Solution{
public:	
	int matSearch (vector <vector <int>> &mat, int N, int M, int X)
	{
	    int row=N-1;
	    int col=0;
	    while(row>=0 && col <= (M-1)){
	        if(mat[row][col]==X){
	            return 1;
	        }
	        else if(mat[row][col]>X){
	            row--;
	        }
	        else{
	            col++;
	        }
	    }
	    return 0;
	}
};
0
aakasshuit2 months ago
//Java Solution

        int row=0;
        int col=M-1;
        while(row<N && col>=0){
            if(mat[row][col]==X){
                return 1;
            }
            if(mat[row][col]<X){
                row++;
            }
            else{
                col--;
            }
        }
        return 0;
+1
singhtanay20002 months ago
// { Driver Code Starts//Initial Template for C
#include <stdio.h>
// } Driver Code Ends//User function Template for C
int matSearch (int N, int M, int matrix[][M], int x){   int row=0,column=M-1;   while(row<N && column>=0){       if(matrix[row][column]==x)           return 1;       else if(matrix[row][column]>x)           column--;       else           row++;          }   return 0;}
// { Driver Code Starts.
int main(){   int tc;scanf("%d", &tc);while(tc--){       int n, m; scanf("%d", &n); scanf("%d", &m); int matrix[n][m];  for(int i = 0; i < n; i++){  for(int j = 0; j < m; j++){   scanf("%d", &matrix[i][j]);  } }     int x; scanf("%d", &x);        int result = matSearch (n, m, matrix, x);       printf("%d", result); printf("\n");   }   return 0;} // } Driver Code Ends
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.