Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
A peak element in a 2D grid is an element that is strictly greater than all of its adjacent neighbors to the left, right, top, and bottom.
Given a 0-indexed m x n matrix mat where no two adjacent cells are equal, find any peak element mat[i][j] and return the length 2 array [i,j].
You may assume that the entire matrix is surrounded by an outer perimeter with the value -1 in each cell.
You must write an algorithm that runs in O(m log(n)) or O(n log(m)) time.
Example 1:
Input: mat = [[1,4],[3,2]] Output: [0,1] Explanation: Both 3 and 4 are peak elements so [1,0] and [0,1] are both acceptable answers.
Example 2:
Input: mat = [[10,20,15],[21,30,14],[7,16,32]] Output: [1,1] Explanation: Both 30 and 32 are peak elements so [1,1] and [2,2] are both acceptable answers.
Constraints:
m == mat.lengthn == mat[i].length1 <= m, n <= 5001 <= mat[i][j] <= 105Problem summary: A peak element in a 2D grid is an element that is strictly greater than all of its adjacent neighbors to the left, right, top, and bottom. Given a 0-indexed m x n matrix mat where no two adjacent cells are equal, find any peak element mat[i][j] and return the length 2 array [i,j]. You may assume that the entire matrix is surrounded by an outer perimeter with the value -1 in each cell. You must write an algorithm that runs in O(m log(n)) or O(n log(m)) time.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[[1,4],[3,2]]
[[10,20,15],[21,30,14],[7,16,32]]
find-peak-element)find-the-peaks)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1901: Find a Peak Element II
class Solution {
public int[] findPeakGrid(int[][] mat) {
int l = 0, r = mat.length - 1;
int n = mat[0].length;
while (l < r) {
int mid = (l + r) >> 1;
int j = maxPos(mat[mid]);
if (mat[mid][j] > mat[mid + 1][j]) {
r = mid;
} else {
l = mid + 1;
}
}
return new int[] {l, maxPos(mat[l])};
}
private int maxPos(int[] arr) {
int j = 0;
for (int i = 1; i < arr.length; ++i) {
if (arr[j] < arr[i]) {
j = i;
}
}
return j;
}
}
// Accepted solution for LeetCode #1901: Find a Peak Element II
func findPeakGrid(mat [][]int) []int {
maxPos := func(arr []int) int {
j := 0
for i := 1; i < len(arr); i++ {
if arr[i] > arr[j] {
j = i
}
}
return j
}
l, r := 0, len(mat)-1
for l < r {
mid := (l + r) >> 1
j := maxPos(mat[mid])
if mat[mid][j] > mat[mid+1][j] {
r = mid
} else {
l = mid + 1
}
}
return []int{l, maxPos(mat[l])}
}
# Accepted solution for LeetCode #1901: Find a Peak Element II
class Solution:
def findPeakGrid(self, mat: List[List[int]]) -> List[int]:
l, r = 0, len(mat) - 1
while l < r:
mid = (l + r) >> 1
j = mat[mid].index(max(mat[mid]))
if mat[mid][j] > mat[mid + 1][j]:
r = mid
else:
l = mid + 1
return [l, mat[l].index(max(mat[l]))]
// Accepted solution for LeetCode #1901: Find a Peak Element II
impl Solution {
pub fn find_peak_grid(mat: Vec<Vec<i32>>) -> Vec<i32> {
let mut l: usize = 0;
let mut r: usize = mat.len() - 1;
while l < r {
let mid: usize = (l + r) >> 1;
let j: usize = mat[mid]
.iter()
.position(|&x| x == *mat[mid].iter().max().unwrap())
.unwrap();
if mat[mid][j] > mat[mid + 1][j] {
r = mid;
} else {
l = mid + 1;
}
}
let j: usize = mat[l]
.iter()
.position(|&x| x == *mat[l].iter().max().unwrap())
.unwrap();
vec![l as i32, j as i32]
}
}
// Accepted solution for LeetCode #1901: Find a Peak Element II
function findPeakGrid(mat: number[][]): number[] {
let [l, r] = [0, mat.length - 1];
while (l < r) {
const mid = (l + r) >> 1;
const j = mat[mid].indexOf(Math.max(...mat[mid]));
if (mat[mid][j] > mat[mid + 1][j]) {
r = mid;
} else {
l = mid + 1;
}
}
return [l, mat[l].indexOf(Math.max(...mat[l]))];
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.