Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Peak Index in a Mountain Array

Medium
binary-searcharray

A mountain array increases strictly to a single peak and then decreases strictly. Given a mountain array arr, return the index of the peak in O(log n).

Input: one line of space-separated integers (a valid mountain). Output: the peak index.

Example 1
Input:
0 1 0
Output:
1
Example 2
Input:
0 10 5 2
Output:
1
Example 3
Input:
3 9 8 6 4
Output:
1

Constraints

  • 3 ≤ arr.length ≤ 10^5
  • arr is guaranteed to be a mountain.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.