Browse curriculum

Search a 2D Matrix II

Search a matrix whose rows and columns are each sorted: start at the top-right corner and drop a whole row or column with every comparison.

Problem Understanding

Search a 2D Matrix II: an m × n matrix has every row sorted left to right and every column sorted top to bottom. Return true if target is in it.

Example: in the 5 × 5 matrix below, target = 5 returns true and target = 20 returns false.

 1  4  7 11 15
 2  5  8 12 19
 3  6  9 16 22
10 13 14 17 24
18 21 23 26 30

The Intuition: A Corner That Points Two Ways

Binary search on the whole grid fails here: row 3 starts at 10 while row 2 ends at 22, so the matrix is not one sorted list, unlike Search a 2D Matrix.

Pick the cell where the two orders disagree: the top-right corner.

  1. Everything to its left is smaller (same row).
  2. Everything below it is bigger (same column).
  3. So if it is too big, the whole column can go; if it is too small, the whole row can go.

Each comparison removes a row or a column, and the cell you land on is again the top-right corner of what is left. At most m + n moves end the search. This is the same idea as binary search: throw away the part that cannot hold the answer, just one line at a time instead of half.

Interactive Walkthrough

The live cell starts at the top-right corner; rows above it and columns to its right are struck out as they are ruled out. Try Bottom-left for the longest path, or Example 2 to watch the walk leave the grid.

Dry Run Table

Input: the matrix above, target = 5

(r, c) Cell Compare Move
(0, 4) 15 15 > 5 left
(0, 3) 11 11 > 5 left
(0, 2) 7 7 > 5 left
(0, 1) 4 4 < 5 down
(1, 1) 5 5 = 5 found

Answer: true after 4 moves.

The Solution Template

Two indexes and one loop. No extra memory.

Search a 2D Matrix II Code
1var searchMatrix = function(matrix, target) {
2 let r = 0, c = matrix[0].length - 1;
3 while (r < matrix.length && c >= 0) {
4 if (matrix[r][c] === target) return true;
5 if (matrix[r][c] > target) c--;
6 else r++;
7 }
8 return false;
9};

Edge Cases & Common Mistakes

  • Starting at the top-left: both neighbours are bigger, so a too-small cell gives no direction. Use the top-right or bottom-left corner.
  • Flattening into one array: that only works for LeetCode 74, where each row starts after the previous one ends.
  • Loop bounds: check r < m and c >= 0 before reading matrix[r][c].
  • Binary search on every row: correct, but O(m log n), which is slower than O(m + n) when the matrix is square.
  • Duplicates: fine; the walk stops at the first copy it meets.

Summary

Start at the top-right corner. Too big: move left and drop the column. Too small: move down and drop the row. Each move removes a whole line, so the search ends in O(m + n) time with O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Start at the top-right

    Set r = 0 and c = n - 1.

  2. Compare and drop a line

    If matrix[r][c] equals the target, return true. If it is bigger, move left (c -= 1); if it is smaller, move down (r += 1).

  3. Stop at the edge

    When r reaches m or c drops below 0, the target is not in the matrix: return false.

Frequently asked questions

Why start at the top-right corner?

The top-right cell is the largest in its row and the smallest in its column. So one comparison always tells you which way to go: if it is bigger than the target, the whole column below it is bigger too and can be dropped; if it is smaller, the whole row to its left is smaller and can be dropped.


Can you start at the bottom-left corner instead?

Yes. The bottom-left cell is the smallest in its row and the largest in its column, so the same argument works with the moves flipped: go up when it is too big and right when it is too small. The top-left and bottom-right corners do not work, because both neighbours move in the same direction.


How is this different from Search a 2D Matrix (LeetCode 74)?

In LeetCode 74 each row starts after the previous row ends, so the whole matrix is one sorted array and binary search gives O(log(m·n)). In LeetCode 240 only rows and columns are sorted on their own, and a row can start below the previous row's end, so the staircase walk in O(m + n) is the standard answer.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description