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
manojyadavblack1 day ago
//I have explained 3 methods to solve this problem 
//   BRute---> Beetter---->Optimal

//This function is associated with method -2
int performBinarySearch(int arr[], int target, int l , int h)
{
    if(l<=h)
    {
        int mid=(l+h)/2;
        if(target==arr[mid]) return mid;
        else if(target>arr[mid]) return performBinarySearch(arr,target,mid+1,h);
        else return performBinarySearch(arr,target,l,mid-1);
    }
    return -1;
}


int matSearch (int N, int M, int matrix[][M], int target)
{
    int row=N;
    int col=M;
    
    //Method -1
     //Brutefprce approach
    // for(int i=0;i<N;i++)
    // for(int j=0;j<col;j++)
    // if(X==matrix[i][j])  return 1;
    // return 0;
    
    
    //Method -2
    // Performing Binary search row wise
    // for(int i=0;i<row;i++)
    // {
    //     int index=performBinarySearch( matrix[i], target, 0,col-1);
    //     if(index!=-1) return 1;
        
    // }
    
    // return 0;
    
    
    //Method-3
    //Performing Binary seacrh using given property s.t that matrix is row and col wise sperted
    int i=0;
    int j=col-1;
    while(i<row && col>=0)
    {
        if(matrix[i][j]==target) return 1;
        else if(target>matrix[i][j]) i++;
        else j--;
    }
    return 0;
    
    
    
}




0
imjunior4712 days ago
       for(int r = 0; r<N; r++){           for(int c = M-1; c>=0; c--){               if(X > mat[r][c]) break;               if(X == mat[r][c]) return 1;           }       }       return 0;
0
snehabaipalli2 days ago
Python Solution:
class Solution:def matSearch(self,mat, N, M, X): # Complete this function count=0 for i in range(N):     for j in range(M):         if(mat[i][j]==X):             count=1             break if(count==0):     return 0 elif(count==1):     return 1
-3
aamitprasad6185 days ago
CPP
vector<int>arr;
	    
	    for(int i=0;i<N;i++){
	        for(int j=0;j<M;j++){
	            arr.push_back(mat[i][j]);
	        }
	    }
	    
	    for(int i=0;i<arr.size();i++){
	        if(arr[i]==X){
	            return 1;
	        }
	    }
	    
	    return 0;
0
abhishek0908022 weeks ago
    int matSearch (vector <vector <int>> &mat, int N, int M, int X)
    {
        int i=0; int j=M-1; 
        while(i<N and j>=0)
        {
            int temp=mat[i][j]; 
            if(temp<X)
            i++; 
            else if(temp>X)
            j--;
            else if(temp==X)
            return 1; 
        }
        return 0;
    }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.