VOOZH about

URL: https://www.geeksforgeeks.org/dsa/search-in-a-matrix-or-2d-array/

⇱ Search in a Matrix or 2D Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


Output
Found


Time Complexity : O(n * m), where n and m are the rows and column of matrix.
Auxiliary Space : O(1)



Comment
Article Tags:
Article Tags: