Search a 2D Matrix - Problem

Imagine you have a perfectly organized 2D matrix where every row is sorted in ascending order, and each row starts with a number greater than the last number of the previous row. This creates a unique property: if you were to "flatten" this matrix into a single array by reading it row by row, the result would be a completely sorted array!

Your task is to determine whether a given target value exists in this specially structured matrix. The catch? You must solve it in O(log(m × n)) time complexity - essentially treating the 2D matrix as if it were a sorted 1D array and using the power of binary search.

Example:
Matrix: [[1,3,5,7],[10,11,16,20],[23,30,34,60]]
Target: 3 → Return true
Target: 13 → Return false

Input & Output

example_1.py — Basic Search Found
$ Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
💡 Note: The target 3 exists in the matrix at position [0,1]. The binary search efficiently locates it by treating the matrix as a flattened sorted array.
example_2.py — Basic Search Not Found
$ Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false
💡 Note: The target 13 does not exist in the matrix. Binary search determines this efficiently by eliminating half the search space in each iteration.
example_3.py — Single Element Matrix
$ Input: matrix = [[1]], target = 1
Output: true
💡 Note: Edge case with a 1x1 matrix. The single element equals the target, so we return true.

Visualization

Tap to expand
2D Matrix13571011162023303460Flatten1D Array (Conceptual)1031527310411516620723830934106011Key Formula:1D index → 2D coordinates:row = index ÷ colscol = index % colsExample: index 5 → row = 5÷4 = 1, col = 5%4 = 1matrix[1][1] = 11
Understanding the Visualization
1
Matrix Structure
Each row is sorted, and each row starts greater than the previous row's end
2
Conceptual Flattening
If we read the matrix row by row, we get a completely sorted array
3
Index Mapping
Any 1D index can be mapped to 2D coordinates using division and modulo
4
Binary Search
Apply standard binary search using the coordinate conversion
Key Takeaway
🎯 Key Insight: The structured matrix property allows us to perform binary search in O(log(m×n)) time by treating it as a sorted 1D array with smart coordinate conversion.

Time & Space Complexity

Time Complexity
⏱️
O(m×n)

We potentially visit every element in the m×n matrix

n
2n
Linear Growth
Space Complexity
O(1)

Only using a constant amount of extra space for variables

n
2n
Linear Space

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 ≤ m, n ≤ 100
  • -104 ≤ matrix[i][j], target ≤ 104
  • Matrix is sorted: Each row sorted in non-decreasing order
  • Matrix is structured: First integer of each row > last integer of previous row
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28 Apple 22
42.0K Views
High Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen