Find a Peak Element II - Problem

Imagine you're standing on a 2D mountain range represented as a matrix, where each cell contains an elevation value. Your goal is to find a peak element - a position that is strictly higher than all of its neighboring positions (left, right, top, and bottom).

Given an m x n matrix mat where no two adjacent cells have equal values, find any peak element and return its coordinates [i, j].

Important: The matrix is surrounded by an imaginary border of -1 values, making edge and corner elements easier to be peaks. You must solve this efficiently in O(m log n) or O(n log m) time complexity.

Example: In matrix [[1,4,3],[2,5,1]], element 5 at position [1,1] is a peak because it's greater than all neighbors (4, 3, 2, 1).

Input & Output

example_1.py โ€” Basic 2x3 Matrix
$ Input: mat = [[1,4,3],[2,5,1]]
โ€บ Output: [1,1]
๐Ÿ’ก Note: Element 5 at position [1,1] is greater than all its neighbors: up(4), down(-1), left(2), right(1). Since the matrix is surrounded by -1, the 'down' neighbor is -1.
example_2.py โ€” Larger Matrix
$ Input: mat = [[1,4,3,2],[3,6,2,1],[2,1,5,4]]
โ€บ Output: [1,1]
๐Ÿ’ก Note: Element 6 at position [1,1] is a peak because it's greater than neighbors: up(4), down(1), left(3), right(2). Multiple peaks may exist, but we only need to find one.
example_3.py โ€” Single Element
$ Input: mat = [[7]]
โ€บ Output: [0,0]
๐Ÿ’ก Note: In a 1x1 matrix, the single element is always a peak since it's surrounded by -1 values on all sides.

Visualization

Tap to expand
Binary Search Peak FindingMiddle Row SearchPeak!Key Insight: Always move toward steeper terrainโ€ข Find max in middle rowโ€ข If not peak, follow upward directionโ€ข Eliminate half the search space
Understanding the Visualization
1
Divide & Conquer
Start by examining the middle row of the mountain range
2
Find Local Maximum
In that row, find the highest point (maximum element)
3
Check Peak Status
See if this point is higher than all neighboring areas
4
Navigate Smartly
If not a peak, move toward the steepest upward direction
5
Eliminate Half
This eliminates half the search area, just like binary search
Key Takeaway
๐ŸŽฏ Key Insight: Just like climbing a mountain, if you're not at a peak, there's always a direction that goes upward. Follow it using binary search principles to efficiently eliminate half the search space in each iteration!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(mร—n)

We visit every cell once and check up to 4 neighbors for each

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 โ‰ค m, n โ‰ค 500
  • -105 โ‰ค mat[i][j] โ‰ค 105
  • No two adjacent cells are equal
  • You must achieve O(m log n) or O(n log m) time complexity
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
42.0K Views
Medium-High Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen