Locate Local Minima Using Divide-and-Conquer
Heaps, Sorting & SearchModel answerLast updated
Implement sublinear algorithms to identify a local minimum in a 1D array and a 2D grid by applying binary search to unimodal descent.
Overview
An element in an array or grid is called a local minimum if it is strictly smaller than every adjacent neighbor. All values in the input are guaranteed to be distinct, ensuring at least one local minimum always exists.
Your task is to write two functions:
-
find_local_min_1d(arr: list[int]) -> int
Return the index of any local minimum in a one-dimensional array. An elementarr[i]qualifies as a local minimum when it is strictly less thanarr[i-1](ifi > 0) and strictly less thanarr[i+1](ifi < len(arr) - 1).
Required runtime:O(log n) -
find_local_min_2d(matrix: list[list[int]]) -> tuple[int, int]
Return the coordinates(row, col)of any local minimum in ann × mgrid. An elementmatrix[r][c]is a local minimum when it is strictly smaller than all orthogonally adjacent cells (up, down, left, right) that exist within bounds.
Required runtime:O(n log m)orO(n + m)
Function Signatures
def find_local_min_1d(arr: list[int]) -> int:
pass
def find_local_min_2d(matrix: list[list[int]]) -> tuple[int, int]:
pass
Input Specification
arr: A list of integers with lengthn ≥ 1matrix: A 2D list of integers with dimensionsn × mwheren ≥ 1andm ≥ 1- All elements within a single input are pairwise distinct
- Return any valid local minimum when multiple exist
Constraints
1 ≤ n, m ≤ 200,000- For the 2D case:
1 ≤ n × m ≤ 1,000,000 -10⁹ ≤ arr[i], matrix[r][c] ≤ 10⁹
Examples
Example 1: One-Dimensional Array
arr = [9, 7, 8, 5, 6, 4, 10]
# Valid outputs: 1 (value 7), 3 (value 5), or 5 (value 4)
# Returning index 1 is acceptable since 7 < 9 and 7 < 8
Example 2: Two-Dimensional Grid
matrix = [
[10, 8, 11, 15],
[ 9, 6, 7, 14],
[12, 13, 5, 3],
[16, 17, 4, 2]
]
# Valid outputs include (1, 1) where value 6 < 8, 9, 7, 13
# or (3, 3) where value 2 < 3, 4
Edge Cases to Consider
- Single-element inputs (
n = 1or1 × 1grids) are trivially local minima - Monotonically increasing or decreasing sequences place the minimum at a boundary
- Degenerate grids (
1 × morn × 1) should reduce naturally to the one-dimensional approach
Hints
Hint 1
For 1D: even though the array isn't sorted, you can still use binary search by observing the direction values are decreasing.
Hint 2
In 2D, apply binary search across one dimension (columns) while scanning the other (rows) to find a candidate in each iteration.
Hint 3
Maintain an invariant that guarantees a local minimum exists within your search window at every step.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
One-Dimensional Case
Although the array is unsorted, binary search remains applicable. At each midpoint mid, examine whether arr[mid] is greater than arr[mid+1]:
- If
arr[mid] > arr[mid+1]: values descend to the right, so a local minimum must exist in[mid+1, hi] - Otherwise:
arr[mid] < arr[mid+1], meaning values descend to the left, so a local minimum exists in[lo, mid]
This halves the search space each iteration. When the window narrows to a single index, that index is guaranteed to be a local minimum.
Invariant: The search window [lo, hi] always contains a local minimum. Initially the entire array satisfies this. Each split preserves the property because we move toward the descending direction.
Two-Dimensional Case
Apply binary search across columns while scanning rows:
- Choose the middle column
mid - Find the smallest element
v = matrix[r][mid]in that column by scanning all rows (costsO(n)) - Since
vis the column minimum, it already beats any vertical neighbors - Check horizontal neighbors:
- If
matrix[r][mid-1] < v, recurse on columns[lo, mid-1] - Else if
matrix[r][mid+1] < v, recurse on columns[mid+1, hi] - Otherwise
vbeats all four neighbors; return(r, mid)
- If
Invariant: The column range [lo, hi] contains a local minimum. Each column at the boundary has a "witness" element smaller than everything in the neighboring excluded column. When we move left because matrix[r][mid-1] < v, the witness in column mid-1 is smaller than the entire column mid (since v was the column minimum). This maintains the invariant for the new range [lo, mid-1].
The invariant also ensures we never step outside valid bounds. If mid == lo and lo > 0, the witness in column lo must be ≤ v, and that witness is smaller than everything in column lo-1, so we cannot move left. The window never becomes empty.
Complexity Analysis
| Function | Time Complexity | Space Complexity |
|---|---|---|
| 1D | O(log n) | O(1) |
| 2D | O(n log m) | O(1) |
The 2D function performs O(log m) iterations, each scanning O(n) rows to find the column minimum.
Implementation
def find_local_min_1d(arr: list[int]) -> int:
lo, hi = 0, len(arr) - 1
while lo < hi:
mid = (lo + hi) // 2
if mid < len(arr) - 1 and arr[mid] > arr[mid + 1]:
# Values decrease to the right
lo = mid + 1
else:
# arr[mid] is smaller than its right neighbor (or is last element)
hi = mid
return lo
def find_local_min_2d(matrix: list[list[int]]) -> tuple[int, int]:
n, m = len(matrix), len(matrix[0])
lo, hi = 0, m - 1
while lo <= hi:
mid = (lo + hi) // 2
# Find the minimum element in column mid
min_row = 0
for i in range(1, n):
if matrix[i][mid] < matrix[min_row][mid]:
min_row = i
v = matrix[min_row][mid]
# Check horizontal neighbors
left_smaller = mid > 0 and matrix[min_row][mid - 1] < v
right_smaller = mid < m - 1 and matrix[min_row][mid + 1] < v
if left_smaller:
hi = mid - 1
elif right_smaller:
lo = mid + 1
else:
# v beats all neighbors
return (min_row, mid)
# Unreachable for valid distinct inputs
return (0, 0)
Common Mistakes
- Trying to sort the array first — this destroys the local structure and makes the problem trivial but violates the runtime requirement
- Checking all neighbors at
midin 1D — only one neighbor check is needed to determine the descent direction - Not maintaining the invariant properly in 2D — forgetting that the column minimum automatically beats vertical neighbors
- Off-by-one errors — carefully handle boundary conditions when
midis atloorhi - Using the global minimum in 2D — while the global minimum is always a local minimum, finding it requires
O(nm)time; the column-scan approach is more efficient
Edge Case Handling
- Single element: Both functions return immediately without entering loops
- Monotonic sequences: The 1D search drifts to index 0 or n-1; non-existent neighbors are not compared
- 1 × m matrices: Each column scan is O(1), reducing to the 1D algorithm
- n × 1 matrices: Single column scan finds the global minimum, which is trivially local
For Example 2, scanning the middle column [8, 6, 13, 17] finds minimum 6 at row 1. Both neighbors (9 and 7) are larger, so (1, 1) is returned immediately as a valid answer.