Search a 2D Matrix
Signals to notice
Brute force first
Linear scan of all m × n elements. Ignores the sorted structure completely. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.
The key insight
Treat the 2D matrix as a flattened 1D sorted array. Binary search with index mapping: row = mid / cols, col = mid % cols. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.
What must stay true
Because the first element of each row is greater than the last element of the previous row, the matrix IS a sorted array when read left-to-right, top-to-bottom. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.
Easy way to go wrong
Doing two binary searches (find row, then find column) — works but is unnecessary. A single binary search on the flattened index is simpler and just as fast. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.