Given a 2D integer array mat[][] and a number x, find whether element x is present in the matrix or not.
Examples:
Input: mat[][] = [[6, 23, 21], [4, 45, 32], [69, 11, 87]], x = 32
Output: true
Explanation: 32 is present in the matrix.
Input: mat[][] = [[14, 34, 23, 95, 43, 28]], x = 55
Output: false
Explanation: 55 is not present in the matrix.
We traverse all elements row by row and compare each element with the target
x. If a match is found, we returntrue; otherwise, after completing the traversal, we returnfalse.
Let us understand with an example:
Input: mat = [[6, 23, 21], [4, 45, 32], [69, 11, 87]], x = 32
Start from first row:
- At (0, 0) -> value = 6, not equal to x -> move right
- At (0, 1) -> value = 23, not equal to x -> move right
- At (0, 2) -> value = 21, not equal to x -> move to next row
Move to second row:
- At (1, 0) -> value = 4, not equal to x -> move right
- At (1, 1) -> value = 45, not equal to x -> move right
- At (1, 2) -> value = 32, equal to x -> element found, return true
Traversal stops as soon as an element is found. Final Output: true
#include <iostream>
#include <vector>
using namespace std;
// Function to search for the target in mat
bool searchMatrix(vector<vector<int>> &mat, int x)
{
// Number of rows
int n = mat.size();
// Number of columns
int m = mat[0].size();
// Perform a linear search in the unsorted mat
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mat[i][j] == x)
{
// Found the target
return true;
}
}
}
// Element not found
return false;
}
// Driver Code
int main() {
vector<vector<int>> mat = {
{6, 23, 21},
{4, 45, 32},
{69, 11, 87}
};
int x = 32;
cout << (searchMatrix(mat, x)? "true" : "false");
return 0;
}
#include <stdio.h>
#include <stdbool.h>
#define MAXR 100
#define MAXC 100
// Function to search for the target in mat
bool searchMatrix(int mat[MAXR][MAXC], int n, int m, int x)
{
// Perform a linear search in the unsorted mat
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mat[i][j] == x)
{
// Found the target
return true;
}
}
}
// Element not found
return false;
}
// Driver Code
int main()
{
int mat[MAXR][MAXC] = {
{6, 23, 21},
{4, 45, 32},
{69, 11, 87}
};
int x = 32;
int n = 3;
int m = 3;
printf("%s", searchMatrix(mat, n, m, x)? "true" : "false");
return 0;
}
import java.util.*;
// solution class
class GfG {
public boolean searchMatrix(int[][] mat, int x)
{
// Number of rows
int n = mat.length;
// Number of columns
int m = mat[0].length;
// Perform a linear search in the unsorted
// matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == x) {
// Found the target
return true;
}
}
}
// Element not found
return false;
}
// Driver Code
public static void main(String[] args)
{
int[][] mat = {
{6, 23, 21},
{4, 45, 32},
{69, 11, 87}
};
int x = 32;
GfG obj = new GfG();
System.out.println(obj.searchMatrix(mat, x)? "true" : "false");
}
}
# Function to search for the target in mat
def searchMatrix(mat, x):
# Number of rows
n = len(mat)
# Number of columns
m = len(mat[0])
# Perform a linear search in the unsorted mat
for i in range(n):
for j in range(m):
if mat[i][j] == x:
# Found the target
return True
# Element not found
return False
# Driver Code
if __name__ == "__main__":
mat = [
[6, 23, 21],
[4, 45, 32],
[69, 11, 87]
]
x = 32
print("true" if searchMatrix(mat, x) else "false")
using System;
using System.Collections.Generic;
// solution class
class GfG
{
// Function to search for the target in mat
public static bool searchMatrix(int[,] mat, int x)
{
// Number of rows
int n = mat.GetLength(0);
// Number of columns
int m = mat.GetLength(1);
// Perform a linear search in the unsorted mat
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mat[i, j] == x)
{
// Found the target
return true;
}
}
}
// Element not found
return false;
}
// Driver Code
static void Main()
{
int[,] mat = {
{6, 23, 21},
{4, 45, 32},
{69, 11, 87}
};
int x = 32;
Console.WriteLine(searchMatrix(mat, x)? "true" : "false");
}
}
// Function to search for the target in mat
function searchMatrix(mat, x)
{
// Number of rows
let n = mat.length;
// Number of columns
let m = mat[0].length;
// Perform a linear search in the unsorted mat
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
if (mat[i][j] === x)
{
// Found the target
return true;
}
}
}
// Element not found
return false;
}
// driver code
let mat = [
[6, 23, 21],
[4, 45, 32],
[69, 11, 87]
];
let x = 32;
console.log(searchMatrix(mat, x)? "true" : "false");
Output
true
There are more versions of searching in a matrix, please refer the below articles.