Find Index of Local Minimum Using Binary Search
Heaps, Sorting & SearchModel answerLast updated
Locate a local minimum in an array of integers in logarithmic time while handling flat regions and boundaries.
An element at index $i$ in an array nums of size $n$ is defined as a local minimum if it is less than or equal to its existing neighbors. Formally:
- For $0 < i < n - 1$: $nums[i] \le nums[i-1]$ and $nums[i] \le nums[i+1]$.
- For $i = 0$: $nums[0] \le nums[1]$ (if $n > 1$).
- For $i = n - 1$: $nums[n-1] \le nums[n-2]$ (if $n > 1$).
- If $n = 1$, index 0 is always a local minimum.
Write a function to return the index of a local minimum in nums in $O(\log n)$ time. When encountering equal elements or multiple paths, favor shifting left so that plateaus resolve to the leftmost reachable index.
Function Signature
def find_local_minimum(nums: list[int]) -> int:
Constraints
- $1 \le n \le 10^5$
- $-10^9 \le nums[i] \le 10^9$
Examples
Example 1:
Input: nums = [9, 6, 3, 4, 7]
Output: 2
Explanation: nums[2] = 3, which is <= 6 and <= 4.
Example 2:
Input: nums = [1, 2, 3, 4, 5]
Output: 0
Explanation: nums[0] = 1, which is <= nums[1].
Example 3:
Input: nums = [2, 2, 2]
Output: 0
Explanation: All values are identical. Index 0 is a local minimum.
Hints
Hint 1
Think about the direction of slope at index `mid`. If `nums[mid] <= nums[mid + 1]`, can you guarantee a local minimum exists in the range `[lo, mid]`?
Hint 2
Consider the boundary invariants: if the array enters a subarray going downward from the left and exits going upward to the right, a valley must exist inside.
Hint 3
Is it possible to guarantee finding the absolute globally leftmost local minimum in O(log n) time? Why or why not?
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach & Algorithmic Design
To achieve $O(\log n)$ time complexity, we adapt binary search by examining the local slope between nums[mid] and nums[mid + 1].
Invariant Argument
We maintain a search range $[lo, hi]$ with the following invariant:
- Either $lo = 0$, or $nums[lo - 1] > nums[lo]$ (the sequence decreases entering from the left).
- Either $hi = n - 1$, or $nums[hi] \le nums[hi + 1]$ (the sequence non-decreases leaving to the right).
Any range satisfying this invariant is guaranteed to contain at least one local minimum.
- Initial State: $[lo, hi] = [0, n - 1]$. The boundary rules ensure the invariant holds at start.
- Shrinking the Window:
- Compute
mid = (lo + hi) // 2. Note that since $lo < hi$, we have $mid < hi$, ensuringmid + 1is always a valid index. - If
nums[mid] <= nums[mid + 1]: The sequence is flat or ascending going right. Moving left (or staying atmid) continues along a non-increasing path backwards, guaranteeing a local minimum in $[lo, mid]$. Thus, sethi = mid. - If
nums[mid] > nums[mid + 1]: The sequence is strictly descending going right. Following this slope downward guarantees a local minimum in $[mid + 1, hi]$. Thus, setlo = mid + 1.
- Compute
Clarification on "Smallest Index" vs. $O(\log n)$
It is important to note that no deterministic algorithm can guarantee finding the absolute first (lowest index) local minimum in $O(\log n)$ time for an arbitrary unsorted array.
An adversary can present a strictly decreasing array [n, n-1, ..., 1] (whose only local minimum is at index $n-1$) and insert a deep dip at an unread index $j < n-1$. Because any $O(\log n)$ search skips $O(n)$ elements, it cannot distinguish between the pure decreasing sequence and one with a hidden earlier dip without inspecting every element in $O(n)$ time.
Therefore, the left-bias rule (nums[mid] <= nums[mid + 1] -> hi = mid) breaks ties locally to push the search toward lower indices whenever possible, maintaining $O(\log n)$ complexity while returning valid local minima (e.g., returning 0 for [2, 2, 2]).
Python Code
def find_local_minimum(nums: list[int]) -> int:
"""
Finds the index of a local minimum in O(log n) time.
Uses left-biased binary search to handle plateaus and non-strict inequalities.
"""
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = (lo + hi) // 2
# Compare current element with its right neighbor
if nums[mid] <= nums[mid + 1]:
# Valley or upward slope: search left half including mid
hi = mid
else:
# Downward slope: search right half excluding mid
lo = mid + 1
return lo
Complexity Analysis
- Time Complexity: $\mathcal{O}(\log n)$. The search space $[lo, hi]$ is halved in every iteration.
- Space Complexity: $\mathcal{O}(1)$. Only two scalar pointers (
lo,hi) and an index variable (mid) are used.
Key Edge Cases
| Input Array | Output | Explanation |
|---|---|---|
[10] | 0 | Single element array; loop terminates immediately. |
[1, 2, 3, 4] | 0 | Strictly increasing array; hi shrinks down to index 0. |
[4, 3, 2, 1] | 3 | Strictly decreasing array; lo increments up to n - 1. |
[5, 5, 5, 5] | 0 | Constant array; non-strict comparison keeps pulling hi left. |
[10, 20, 15, 8, 30] | 1 or 3 | Multiple local minima; algorithm converges on one in $O(\log n)$. |
Follow-Up Questions & Trade-Offs
-
What if duplicate values allowed arbitrary plateaus in strict local minima? If local minimum is strictly defined ($nums[i-1] > nums[i] < nums[i+1]$), a plateau like
[2, 2, 2]contains no valid local minimum. If duplicates are present, a slope ofnums[mid] == nums[mid + 1]gives no information about where a strict valley lies, degrading the worst-case time complexity to $O(n)$. -
Rotated Sorted Arrays: Finding the minimum in a rotated sorted array uses a similar binary search comparing
nums[mid]withnums[hi]. If duplicates exist,nums[mid] == nums[hi]requires decrementinghi -= 1, which yields $O(n)$ worst-case performance.