Search in a Matrix or 2D Array
                                        
                                                                                    
                                                
                                                    Last Updated : 
                                                    30 Oct, 2024
                                                
                                                 
                                                 
                                             
                                                                             
                                                             
                            
                            
                                                                                    
                Given a matrix mat[n][m] and an element target. return true if the target is present in the matrix, else return false. 
Examples: 
Input : mat[][] = { {10, 51, 9},
                               {14, 20, 21},
                               {30, 24, 43} }
target = 14
Output: Found 
Input : mat[][] = {{31, 5, 9, 11},
                              {14, 7, 21, 26},
                              {30, 4, 43, 50} }
target = 42
Output: Not Found
We traverse the mat[][] and  compare target with every element of the matrix. If matches, then return true  If we reach the end we will return false.
            C++
    // C++ program to search target in a matrix
#include <bits/stdc++.h>
using namespace std;
bool matrixSearch(vector<vector<int> >& mat, int x)
{
    int n = mat.size(), m = mat[0].size();
    // Compare each element one by one
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (mat[i][j] == x)
                return true;
    return false;
}
int main() {
    vector<vector<int> > mat = { { 1, 5, 9, 11 },
                                 { 14, 20, 21, 26 },
                                 { 30, 34, 43, 50 } };
    if (matrixSearch(mat, 14))
        cout << "Found\n";
    else
        cout << "Not Found\n";
    return 0;
}
            C
    // C program to check position of target Linearly
#include <stdbool.h>
#include <stdio.h>
#define MAX 100
bool matrixSearch(int mat[MAX][MAX], int n, int m,
                        int x)
{
    // Compare each element one by one
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (mat[i][j] == x) {
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int mat[MAX][MAX] = { { 1, 5, 9, 11 },
                          { 14, 20, 21, 26 },
                          { 30, 34, 43, 50 } };
    // Number of rows
  	int n = 3; 
  	
    // Number of columns
    int m = 4; 
  	
    // Element to search
    int x = 14; 
    if (matrixSearch(mat, n, m, x)) {
        printf("Found\n");
    }
    else {
        printf("Not Found\n");
    }
    return 0;
}
            Java
    // Java program to check position of target Linearly
import java.util.*;
class GfG {
    public static boolean matrixSearch(int[][] mat,
                                             int x)
    {
        int n = mat.length;
        int m = mat[0].length;
        // Compare each element one by one
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (mat[i][j] == x) {
                    return true;
                }
            }
        }
        return false;
    }
    public static void main(String[] args)
    {
        int[][] mat = { { 1, 5, 9, 11 },
                        { 14, 20, 21, 26 },
                        { 30, 34, 43, 50 } };
        if (matrixSearch(mat, 14)) {
            System.out.println("Found");
        }
        else {
            System.out.println("Not Found");
        }
    }
}
            Python
    # Python program to check position of target Linearly
def matrixSearch(mat, x):
    n = len(mat)
    m = len(mat[0])
    # Compare each element one by one
    for i in range(n):
        for j in range(m):
            if mat[i][j] == x:
                return True
    return False
if __name__ == "__main__":
    mat = [
        [1, 5, 9, 11],
        [14, 20, 21, 26],
        [30, 34, 43, 50]
    ]
    if matrixSearch(mat, 14):
        print("Found")
    else:
        print("Not Found")
            C#
    // C# program to check position of target Linearly
using System;
class GfG {
    public static bool matrixSearch(int[, ] mat, int x)
    {
        int n = mat.GetLength(0);
        int m = mat.GetLength(1);
        // Compare each element one by one
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (mat[i, j] == x)
                    return true;
        return false;
    }
    public static void Main()
    {
        int[, ] mat = { { 1, 5, 9, 11 },
                        { 14, 20, 21, 26 },
                        { 30, 34, 43, 50 } };
        if (matrixSearch(mat, 14))
            Console.WriteLine("Found");
        else
            Console.WriteLine("Not Found");
    }
}
            JavaScript
    // Javascript program to check position of target Linearly
function matrixSearch(mat, x)
{
    let n = mat.length;
    let m = mat[0].length;
    // Compare each element one by one
    for (let i = 0; i < n; i++)
        for (let j = 0; j < m; j++)
            if (mat[i][j] === x)
                return true;
    return false;
}
let mat = [
    [ 1, 5, 9, 11 ], [ 14, 20, 21, 26 ], [ 30, 34, 43, 50 ]
];
if (matrixSearch(mat, 14))
    console.log("Found");
else
    console.log("Not Found");
Time Complexity : O(n * m), where n and m are the rows and column of matrix.
Auxiliary Space : O(1)
                                
                                
                            
                                                                                
                                                            
                                                    
                                                
                                                        
                            
                        
                                                
                        
                                                                                    
                                                                Explore
                                    
                                        DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem