Matrix search depends on how the matrix is sorted:
Fully sorted row-major → Flatten + Binary Search · Rows and columns sorted → Staircase Search
Focus on recognizing:
“Sorted matrix + find target” → pick the strategy from the sort order
Pattern 1: Flattened Binary Search
When the whole matrix is one sorted sequence, treat it as a 1D array of size rows × cols:
public boolean searchMatrix(int[][] matrix, int target) {
int rows = matrix.length;
int cols = matrix[0].length;
int lo = 0;
int hi = rows * cols - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int val = matrix[mid / cols][mid % cols];
if (val == target) {
return true;
} else if (val < target) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return false;
}def search_matrix(matrix, target):
rows, cols = len(matrix), len(matrix[0])
lo, hi = 0, rows * cols - 1
while lo <= hi:
mid = lo + (hi - lo) // 2
val = matrix[mid // cols][mid % cols]
if val == target:
return True
elif val < target:
lo = mid + 1
else:
hi = mid - 1
return Falsebool searchMatrix(vector<vector<int>>& matrix, int target) {
int rows = matrix.size(), cols = matrix[0].size();
int lo = 0, hi = rows * cols - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int val = matrix[mid / cols][mid % cols];
if (val == target) return true;
else if (val < target) lo = mid + 1;
else hi = mid - 1;
}
return false;
}function searchMatrix(matrix, target) {
const rows = matrix.length,
cols = matrix[0].length;
let lo = 0,
hi = rows * cols - 1;
while (lo <= hi) {
const mid = lo + ((hi - lo) >> 1);
const val = matrix[(mid / cols) | 0][mid % cols];
if (val === target) return true;
else if (val < target) lo = mid + 1;
else hi = mid - 1;
}
return false;
}The trick:
row = mid / cols,col = mid % cols— a virtual 1D index mapped into the grid.
Requires: last element of each row < first element of the next.
Pattern 2: Staircase Search
Watch the staircase hunt for 6 in a row/column-sorted grid — two eliminations down, one left. Press ▶ to animate.
⚠️ Animation & Content Notice
The animation work is not fully finished — some animations may have slight errors.
If there is a major error in the content or if the animation or content is difficult to understand, please contact us at rayyancodingschool@gmail.com.
Search a Sorted Matrix — Staircase (Zigzag) Walk
Search for a target in an m×n matrix where each row is sorted left-to-right and each column is sorted top-to-bottom. Start at the top-right corner and eliminate a whole row or column each step.
Matrix [[1,4,7,11],[2,5,8,12],[3,6,9,16]]; target 6. Start top-right (11): because the column below is even bigger and the row to the left is smaller, comparing once tells you whether to drop the whole column (move left) or whole row (move down). Each step removes a full line → O(m+n).
1
r = 0, c = cols - 1 // top-right corner
2
while r < m && c >= 0:
3
v = matrix[r][c]
4
if v == target: found
5
if v > target: c-- // whole column too big → left
6
else: r++ // whole row too small → down
1
for each row:
2
if row[0] <= target <= row[last]:
3
binary search THIS row
4
if found: return true
5
return false
When only rows AND columns are sorted (not continuously), start at the top-right corner — every step eliminates a full row or column:
public boolean searchMatrix(int[][] matrix, int target) {
int rows = matrix.length;
int col = matrix[0].length - 1;
int row = 0;
while (row < rows && col >= 0) {
int val = matrix[row][col];
if (val == target) {
return true;
} else if (val < target) {
row++; // left of val is even smaller → down
} else {
col--; // below val is even larger → left
}
}
return false;
}def search_matrix(matrix, target):
row, col = 0, len(matrix[0]) - 1
while row < len(matrix) and col >= 0:
val = matrix[row][col]
if val == target:
return True
elif val < target:
row += 1 # left of val is even smaller → down
else:
col -= 1 # below val is even larger → left
return Falsebool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = 0;
int col = matrix[0].size() - 1;
while (row < (int)matrix.size() && col >= 0) {
int val = matrix[row][col];
if (val == target) return true;
else if (val < target) row++;
else col--;
}
return false;
}function searchMatrix(matrix, target) {
let row = 0,
col = matrix[0].length - 1;
while (row < matrix.length && col >= 0) {
const val = matrix[row][col];
if (val === target) return true;
else if (val < target) row++;
else col--;
}
return false;
}Staircase = top-right start. Smaller than target → down. Larger → left.
Common Mistakes
Flattened BS on the wrong matrix.
It needs strict row-major order (row i's last < row i+1's first). Otherwise use staircase.
Index conversion with the wrong divisor.
row = mid / cols, col = mid % cols — dividing by rows scrambles coordinates.
Staircase from the wrong corner.
Top-right works because it’s the only cell that is largest-in-row AND smallest-in-column simultaneously. Bottom-left also works; top-left/bottom-right don’t.
Reversed staircase moves.
val < target → down, val > target → left. Swapping them walks off the matrix.
Complexity
| Strategy | Time | Space |
|---|---|---|
| Flattened | O(log(m·n)) | O(1) |
| Staircase | O(rows+cols) | O(1) |
Premium Content
Unlock Binary Search in Matrix and all premium lessons with a subscription.
From ₹199.99/year — See plans