![]() |
VOOZH | about |
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:
Move to second row:
Traversal stops as soon as an element is found. Final Output: true
true
There are more versions of searching in a matrix, please refer the below articles.