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)
Similar Reads
Prefix Sum of Matrix (Or 2D Array) Given a 2D integer matrix mat[][]and a list of queries queries[][], the objective is to compute the sum of elements within a specific submatrix of the given matrix for each query.Each query is defined by four integers [r1, c1, r2, c2].r1, c1: the top-left coordinate of the submatrixr2, c2: the botto
15+ min read
Prefix Sum of Matrix (Or 2D Array) Given a 2D integer matrix mat[][]and a list of queries queries[][], the objective is to compute the sum of elements within a specific submatrix of the given matrix for each query.Each query is defined by four integers [r1, c1, r2, c2].r1, c1: the top-left coordinate of the submatrixr2, c2: the botto
15+ min read
Prefix Sum of Matrix (Or 2D Array) Given a 2D integer matrix mat[][]and a list of queries queries[][], the objective is to compute the sum of elements within a specific submatrix of the given matrix for each query.Each query is defined by four integers [r1, c1, r2, c2].r1, c1: the top-left coordinate of the submatrixr2, c2: the botto
15+ min read
Search element in Matrix Given an NÃN matrix mat[][] and an integer K, the task is to search K in mat[][] and if found return its indices. Examples: Input: mat[][] = {{1, 2, 3}, {7, 6, 8}, {9, 2, 5}}, K = 6Output: {1, 1}Explanation: mat[1][1] = 6 Input: mat[][] = {{3, 4, 5, 0}, {2, 9, 8, 7}}, K = 10Output: Not Found  Appro
4 min read
Saddleback Search Algorithm in a 2D array Find an element in a given matrix such that each row and each column is sorted. Examples: Input : arr[] = { { 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9} } element=5 Output : Element Found at position (1, 1). Input : arr[]={ { 11, 21, 31, 41, 51 }, { 12, 22, 32, 42, 52 }, { 13, 23, 33, 43, 53 }, { 14, 24, 34,
6 min read
Saddleback Search Algorithm in a 2D array Find an element in a given matrix such that each row and each column is sorted. Examples: Input : arr[] = { { 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9} } element=5 Output : Element Found at position (1, 1). Input : arr[]={ { 11, 21, 31, 41, 51 }, { 12, 22, 32, 42, 52 }, { 13, 23, 33, 43, 53 }, { 14, 24, 34,
6 min read