SDE & MLE Prep

852. Peak Index in a Mountain Array

Problem

arr is a mountain: strictly increasing up to one peak, then strictly decreasing. Return the peak's index in O(log n).

Example: [0,10,5,2] → 1

Find the first index where the array starts descending: arr[mid] > arr[mid + 1].

Complexity: O(log n).

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        lo, hi = 0, len(arr) - 1
        while lo < hi:
            mid = (lo + hi) // 2
            if arr[mid] > arr[mid + 1]:
                hi = mid
            else:
                lo = mid + 1
        return lo