Skip to main content
Medium Binary Search High frequency

Search a 2D Matrix

Open on LeetCode

Approach Summary

Treat the matrix as a flattened sorted array. Binary search on index 0..m*n-1, converting mid to (row, col) via division and modulo.

How to Recognize This Pattern

  • Rows sorted, first element of each row > last of previous
  • Binary search on virtual 1D array

Complexity Analysis

Time Complexity

O(log(m × n))

Space Complexity

O(1)

Tags

Array Binary Search Matrix

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee