Peak Index in a Mountain Array
mediumTime: O(log n)Space: O(1)
Recognize the pattern
peak of mountain arraygoes up then downbinary search on slope
Brute force idea
Linear scan for max — O(n).
Better approach
Binary search: if nums[mid] < nums[mid+1], ascending (peak right). Else descending (peak left/at mid). O(log n).
Key invariant
The array is bitonic. Comparing mid with mid+1 determines which slope you're on.
Watch out for
Comparing with both neighbors — only mid vs mid+1 is needed.