LC 162's simpler cousin — the array is guaranteed to be a mountain. Same slope logic, same template, cleaner guarantees.
An array arr is a mountain array if it strictly increases to a single peak then strictly decreases. Given such an array, return the index of the peak element. Must run in O(log n).
Because the array is a strict mountain, we can also frame this as a First True problem. The condition arr[i] > arr[i+1] is false on the ascending slope and true from the peak onwards.
vs LC 162: LC 162 has multiple peaks and uses the slope-following template. LC 852 has exactly one peak and can use either the slope template or the First True template — they give identical results on a strict mountain array.
class Solution { public int peakIndexInMountainArray(int[] arr) { int left = 0, right = arr.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (arr[mid] < arr[mid + 1]) left = mid + 1; // still ascending — peak is to the right else right = mid; // descending — peak is here or to the left } return left; } }
class Solution: def peakIndexInMountainArray(self, arr: list[int]) -> int: left, right = 0, len(arr) - 1 while left < right: mid = left + (right - left) // 2 if arr[mid] < arr[mid + 1]: left = mid + 1 # still ascending else: right = mid # descending or at peak return left