AlgoMaster Logo

Search a 2D Matrix

Last Updated: March 29, 2026

medium
3 min read

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. This means if you read the matrix row by row, the entire sequence of elements is sorted. We need to determine whether a given target value exists somewhere in this matrix.

The critical insight is that this matrix is essentially 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. So we are really just doing a search in a sorted sequence, which immediately suggests binary search.

Key Constraints:

  • 1 <= m, n <= 100 → The matrix has at most 100 rows and 100 columns, so at most 10,000 elements total. Even an O(m n) scan would be fast, but the problem requires O(log(m n)).
  • Rows are sorted, and first element of each row > last element of previous row → The entire matrix is globally sorted when read row by row. This is exactly the structure binary search needs.
  • -10^4 <= matrix[i][j] <= 10^4 → Standard integer range, no overflow concerns.

Approach 1: Brute Force (Linear Scan)

Intuition

The simplest approach: just check every element in the matrix. Iterate through each row and each column, and if you find the target, return true. If you exhaust the entire matrix without finding it, return false.

This does not use the sorted property at all, which is wasteful, but it establishes a baseline and makes the logic crystal clear.

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 entire matrix is one long sorted sequence when read row by row.

So we can run a standard binary search on indices 0 through m*n - 1. The trick is converting a 1D index mid to a 2D position: the row is mid / n and the column is mid % n, where n is the number of columns. This lets us look up the actual matrix value at any virtual 1D position in O(1).

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