Peak Index in a Mountain Array

Medium

Topics
ArrayBinary Search

An array nums is a mountain: it strictly increases up to a unique peak and then strictly decreases. Return the index of the peak element. Solve in O(log n) time.

Example 1

010
The values strictly rise then strictly fall, so the peak is at index 1.
Bars are the array values, left to right.
Input:  nums = [0,1,0]
Output: 1

Example 2

0210
Input:  nums = [0,2,1,0]
Output: 1

Example 3

01052
Input:  nums = [0,10,5,2]
Output: 1

Constraints

  • 3 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^6
  • nums forms a mountain (unique peak).
Editorial — intuition, approach, complexity and pitfalls

Intuition

The array rises then falls, so the comparison nums[mid] < nums[mid+1] says which side of the peak you are on: still rising means the peak is strictly to the right, falling means mid is the peak or the peak is to its left. That is a monotone predicate over the index, which is all binary search needs — the array itself is not sorted.

Keep hi as a candidate rather than excluding it. Using hi = mid on the falling side and lo = mid + 1 on the rising side converges on the single index that is never ruled out.

Approach

  1. Set lo = 0 and hi = len(nums) - 1.
  2. While lo < hi, take mid and compare it with its right neighbour.
  3. Rising: the peak is above mid, so lo = mid + 1.
  4. Falling: mid could be the peak, so hi = mid.
  5. Return lo.

Dry Run

nums = [0,2,5,7,4,3,1]

  lo  hi  mid  nums[mid]  nums[mid+1]  verdict
  0   6   3    7          4            falling -- peak is here or left
  0   3   1    2          5            rising  -- peak is right
  2   3   2    5          7            rising  -- peak is right

lo == hi == 3   answer 3

Complexity

  • Time — O(log n): the range halves every iteration.
  • Space — O(1).

Common Pitfalls

  • Reaching for a linear scan. It passes, but the problem names O(log n) explicitly, so the linear answer is a wrong answer here.
  • Comparing nums[mid] against nums[mid-1] and nums[mid+1] both and returning early. It works, but it needs two bounds checks and the single-neighbour comparison makes the invariant easier to state.
  • Initialising hi = len(nums). On a guaranteed mountain this is latent rather than fatal — every strict mountain up to length 9 still returns the right index, because the peak can never be the last element so lo never reaches n-1 and nums[mid+1] stays in range. It bites immediately on the sibling problem find peak element, where no mountain is promised. Set the bound correctly rather than relying on the guarantee.
  • Mixing hi = mid with a while lo <= hi loop, which never terminates.
  • Returning mid after the loop. mid is stale; the converged bound is lo.

Full Solution — all languages

class Solution:
    def peakIndexInMountainArray(self, nums):
        lo, hi = 0, len(nums) - 1
        while lo < hi:
            mid = (lo + hi) // 2
            if nums[mid] < nums[mid + 1]:
                lo = mid + 1
            else:
                hi = mid
        return lo
SensAI
Beta
Tap the mic to stop voice mode