VOOZH about

URL: https://www.geeksforgeeks.org/dsa/search-element-sorted-matrix/

⇱ Search in a sorted matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Search in a sorted matrix

Last Updated : 21 Mar, 2026

You are given a 2D matrix mat[][] of size n × m, where:

  • Each row is sorted in increasing order.
  • The first element of each row is greater than or equal to the last element of the previous row (i.e., mat[i][0] ≥ mat[i−1][m−1] for all 1 ≤ i < n).

Given an integer x, check whether x is present in the matrix or not.

Examples:

Input: x = 14, mat[][] = [[ 1, 5, 9],
[14, 20, 21],
[30, 34, 43]]
Output: true
Explanation: The value 14 is present in the second row, first column of the matrix.

Input: x = 42, mat[][] = [[ 1, 5, 9, 11],
[14, 20, 21, 26],
[30, 34, 43, 50]]
Output: false
Explanation: x is not present in matrix.

[Naive Approach] By Traversing all elements – O(n × m) Time and O(1) Space

The idea is to iterate through the entire matrix mat[][] and compare each element with x. If an element matches the x, we will return true. Otherwise, at the end of the traversal, we will return false.

Follow the steps to implement:

  • Iterate through each element of the matrix.
  • Compare each element with x.
  • If a match is found, return true; otherwise, return false.

Output
true

[Better Approach] Using Binary Search Twice - O(log n + log m) Time and O(1) Space

To search efficiently, we first use binary search to find the row where the target x might be located. This is done by comparing x with the first element of the middle row in each step. Once the possible row is identified, we then perform a second binary search within that row to check if x is present.

Step By Step Implementations:

  • Start by setting low = 0 and high = n - 1.
  • Find the middle row using (low + high) / 2.
  • If x is smaller than the first element of the middle row, then x cannot be in that row or any row below it. So, move your search to the rows above by setting high = mid - 1.
  • If x is greater than or equal to the first element of the middle row, then it might be in that row or one of the rows below. So, store this row as a possible candidate and move your search downward by setting low = mid + 1.
  • After finding the correct row where x could be, just do a simple binary search in that row to check if x is present.

Output
true

[Expected Approach] Using Binary Search Once - O(log(n × m)) and O(1) Space

The idea is to consider the given matrix as 1D array and apply only one binary search.
For example, for a matrix of size n x m and we can consider it as a 1D array of size n*m, then the first index would be 0 and last index would n*m-1. So, we need to do binary search from low = 0 to high = (n*m-1).

How to find the element in 2D matrix corresponding to index = mid?

Since each row of mat[][] will have m elements, so we can find the row of the element as (mid / m) and the column of the element as (mid % m). Then, we can compare x with arr[mid/m][mid%m] for each mid and complete our binary search.


Output
true
Comment