AlgoMaster Logo

Search a 2D Matrix

medium5 min readUpdated June 23, 2026

Understanding the Problem

We have a 2D matrix where each row is sorted left to right, and the last element of any row is smaller than the first element of the next row. Reading the matrix row by row produces one fully sorted sequence of elements. We need to determine whether a given target value exists somewhere in this matrix.

This matrix is a sorted 1D array that has been wrapped into rows. The two properties guarantee a strict global ordering: every element in row i is smaller than every element in row i+1. That makes the problem a search over a sorted sequence, which binary search handles in logarithmic time.

Key Constraints:

  • 1 <= m, n <= 100 → At most 10,000 elements, so an O(m n) scan would finish quickly, but the problem requires O(log(m n)).
  • Rows are sorted, and the first element of each row exceeds the last element of the previous row → The matrix is globally sorted when read row by row, which is the structure binary search needs.
  • -10^4 <= matrix[i][j] <= 10^4 → Values fit in a 32-bit integer, so m * n and intermediate index math cannot overflow.

Approach 1: Brute Force (Linear Scan)

Intuition

Check every element in the matrix. Iterate through each row and each column, return true when the target appears, and return false after scanning the whole matrix without a match.

This ignores the sorted property, so it does more work than necessary, but it establishes a correct baseline to optimize from.

Algorithm

  1. Iterate through each row i from 0 to m - 1.
  2. For each row, iterate through each column j from 0 to n - 1.
  3. If matrix[i][j] == target, return true.
  4. If the loop completes without finding the target, return false.

Example Walkthrough

1Start scan: i=0, j=0, looking for target=13
0
1
2
3
0
1
3
5
7
1
10
11
16
20
2
23
30
34
60
1/5

Code

This approach works but ignores the sorted structure entirely. Since the matrix is globally sorted when read row by row, we can treat it as a virtual 1D array and apply binary search.

Approach 2: Binary Search on Virtual 1D Array

Intuition

Because each row is sorted and the first element of each row is greater than the last element of the previous row, the matrix is one long sorted sequence when read row by row.

We run a standard binary search over the indices 0 through m*n - 1 of that virtual sequence. To read the actual value at a virtual index mid, convert it to a 2D position: the row is mid / n and the column is mid % n, where n is the number of columns. That conversion is O(1), so each binary search step looks up its midpoint value in constant time.

Algorithm

  1. Set left = 0 and right = m * n (one past the last index).
  2. While left < right:
    • Compute mid = left + (right - left) / 2.
    • Convert mid to 2D: row = mid / n, col = mid % n.
    • If matrix[row][col] == target, return true.
    • If matrix[row][col] < target, set left = mid + 1.
    • Otherwise, set right = mid.
  3. Return false (target not found).

Example Walkthrough

1Initialize: left=0, right=12, search entire virtual array
0
1
left
1
3
2
5
3
7
4
10
5
11
6
16
7
20
8
23
9
30
10
34
11
60
search range
1/7

Code