Skip to content
AI360Xpert
Beta
Difficulty: MediumBinary Search

Search a 2D Matrix

Problem in Plain English

You are given an m by n matrix where each row is sorted and every row starts after the previous row ends, plus a target integer. Return true when the target appears in the matrix and false otherwise.

Problem Statement

You are given an m x n integer matrix matrix with two properties: each row is sorted in non-decreasing order, and the first integer of each row is greater than the last integer of the previous row.

Given an integer target, return true if target is in matrix or false otherwise. The required solution runs in O(log(m∗n))O(log(m * n)) time.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • -10^4 <= matrix[i][j], target <= 10^4

Examples

Example 1

Input
matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output
true

The value 3 sits at row 0, column 1, so the answer is true.

Example 2

Input
matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output
false

The target 13 lies between 11 and 16 in the second row. Neither cell equals 13, and the row boundaries rule out every other row, so return false.

Example 3

Input
matrix = [[-5]], target = -5
Output
true

The minimum-size matrix has one candidate cell, and its value equals the negative target. The inclusive search interval must examine that cell and return true.

Example 4

Input
matrix = [[1,1,3],[5,5,8]], target = 5
Output
true

Duplicates within a row are allowed. Both 5 values occur in row 1, so finding either is sufficient; strict separation between rows still makes the virtual sequence sorted.

Intuition

Reading each row left to right and then moving to the next row gives one sorted sequence: the next row begins above the previous row's last value. In Example 1 the virtual list is [1,3,5,7,10,11,16,20,23,30,34,60]. Binary search repeatedly halves a candidate interval; integer division by the four columns converts a virtual position to its row, and the remainder gives its column. The list is a way to reason about the input, so there is no need to allocate it.

Approaches

Linear Scan

Solution Details

Reveal Linear Scan: intuition, complexity, and code

Hints

Hint 1
Read the two sorting rules together: the last value of each row sits below the first value of the next row, so the matrix is one sorted sequence.
Hint 2
A flat position mid in a row of n columns converts to row mid / n and column mid % n with integer division.
Hint 3
Run standard binary search on the range 0 to m * n - 1 and compare matrix[row][col] against target at each step.

Edge Cases

  • A single-cell matrix such as [[1]] works because left and right both start at 0.
  • A target smaller than the first cell or larger than the last cell exhausts the range and returns false.
  • A single-row or single-column matrix still maps correctly since the divisor n equals the true column count.

Common Mistakes and Interview Tips

  • Applying this virtual binary search to a matrix that has only individually sorted rows. For [[1,4],[2,5]], row boundaries break the global order, so this method can discard a present target.
  • Dividing mid by the row count m instead of the column count n, or swapping quotient and remainder, maps rectangular matrices to the wrong cell.
  • Using ordinary division in Python or JavaScript produces a fractional row; use // or Math.floor. Java int division already truncates nonnegative indices.
  • Allocating a flattened list first adds O(m * n) copying time and space, defeating the required logarithmic search and constant extra storage.
  • Using left < right skips the final candidate; updating a bound to mid rather than mid + 1 or mid - 1 can stall on a small interval.
  • Assuming rows are strictly increasing rejects valid duplicates within a row. Non-decreasing order is enough; separation between rows is strict. The stated input is nonempty, so matrix[0] is valid.

Key Takeaway

When a 2D grid flattens into one sorted sequence, binary search over virtual indices with arithmetic mapping beats scanning every cell.