Browse curriculum

Find a Peak Element II

Find any cell in a 2D grid that is strictly larger than its four neighbours, by binary searching on columns and comparing each column's maximum with its left and right neighbours.

Problem Understanding

Find a Peak Element II (LeetCode 1901): in an m × n grid where no two adjacent cells are equal, return the position [i, j] of any cell strictly greater than its up, down, left and right neighbours. The grid is surrounded by -1, and the solution must run in O(m log n) or O(n log m).

Examples: mat = [[1,4],[3,2]] → [0,1] or [1,0]; mat = [[10,20,15],[21,30,14],[7,16,32]] → [1,1] or [2,2].

The Intuition: Collapse Each Column to Its Maximum

A column's maximum already beats the cells above and below it, so the 2D problem becomes the 1D one:

  1. Pick the middle column and find its maximum.
  2. Look sideways. If a neighbour in the same row is bigger, climbing toward it can never come back, so a peak lies on that side.
  3. Otherwise the maximum beats all four neighbours: it is a peak.

This is Find Peak Element applied to column maxima, and it uses the same "discard the half that cannot hold the answer" step as Search a 2D Matrix.

Interactive Walkthrough

NUMBERS UNDER A CELL = ROW, COLUMN

ALL COLUMNS

41

0,0

45

0,1

8

0,2

37

0,3

30

0,4

13

0,5

31

0,6

52

1,0

15

1,1

20

1,2

40

1,3

38

1,4

48

1,5

47

1,6

1

2,0

50

2,1

53

2,2

36

2,3

59

2,4

55

2,5

2

2,6

54

3,0

28

3,1

9

3,2

49

3,3

56

3,4

10

3,5

34

3,6

lo

0

hi

6

mid

:

Peak

:

 

Search columns 0 to 6. Every cell outside the grid counts as -1

Next

Column 3: its maximum is 49 (row 3), already bigger than the cells above and below

Free preview: the first 3 of 7 steps

mat

Struck-out columns are ruled out, the tinted column is the middle one and the filled cell is its maximum, compared with the two cells beside it. Try Goes left to see the search move the other way.

Dry Run Table

Input: the default 4 × 7 grid in the walkthrough.

lo..hi mid Column max (row) Left Right Move
0..6 3 49 (3) 9 56 right
4..6 5 55 (2) 59 2 left
4..4 4 59 (2) 36 55 peak

Answer: [2, 4] (value 59).

The Solution Template

Binary search on columns; one O(m) scan per step.

Find a Peak Element II Code
1var findPeakGrid = function(mat) {
2 const m = mat.length, n = mat[0].length;
3 let lo = 0, hi = n - 1;
4 while (lo <= hi) {
5 const mid = (lo + hi) >> 1;
6 let row = 0;
7 for (let r = 1; r < m; r++) if (mat[r][mid] > mat[row][mid]) row = r;
8 const left = mid > 0 ? mat[row][mid - 1] : -1;
9 const right = mid < n - 1 ? mat[row][mid + 1] : -1;
10 if (left > mat[row][mid]) {
11 hi = mid - 1;
12 } else if (right > mat[row][mid]) {
13 lo = mid + 1;
14 } else {
15 return [row, mid];
16 }
17 }
18 return [-1, -1];
19};

Edge Cases & Common Mistakes

  • Using any cell of the middle column: only its maximum is guaranteed to beat its vertical neighbours.
  • Edges: a missing neighbour counts as -1, so the first or last column can hold the peak.
  • One column: its maximum is a peak immediately.
  • Moving toward the smaller neighbour: the climb argument only works toward a bigger one.
  • Several peaks: any one is accepted; the search returns the first it lands on.

Summary

Binary search on columns: take the middle column's maximum, move toward a bigger left or right neighbour, and stop when neither is bigger. O(m log n) time, O(1) space.

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

The approach, step by step

  1. Search columns

    lo = 0, hi = n - 1; take mid = (lo + hi) / 2.

  2. Column maximum

    Find the row with the largest value in column mid.

  3. Compare sideways

    If the left neighbour is bigger, hi = mid - 1; if the right one is, lo = mid + 1; otherwise return [row, mid].

Frequently asked questions

Why use the maximum of the middle column?

The column's maximum is already bigger than the cells above and below it, so only its left and right neighbours can stop it from being a peak. Any other cell in the column could lose to a vertical neighbour, and the argument for throwing away half the columns would no longer hold.


Why is it safe to drop the other half?

Say the left neighbour L is bigger than the column maximum. Every cell in the left half that you could reach by climbing to bigger values is at least L, which is bigger than every value in the middle column. So a climb that starts at L never crosses back into the middle column, and since the grid is finite it ends at a peak inside the left half.


How is this related to Find Peak Element (LeetCode 162)?

It is the same binary search on a 1D array, with each column collapsed to its maximum. In 1D you compare nums[mid] with nums[mid + 1]; here you compare a column's maximum with the cells beside it in the same row.


Can you do better than O(m log n)?

Searching on rows instead gives O(n log m), so pick the shorter side to scan: O(min(m, n) · log(max(m, n))). There is an O(m + n) divide-and-conquer that halves both dimensions, but it is rarely expected in interviews.

New to the technique behind this problem? Watch it run step by step:

Binary Search Visualization

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

AI resume checker: match your resume to the job description