 All Problems
Find Peak Element
medium
binary search
arrays
google
microsoft
facebook

A peak element is one that is strictly greater than its neighbors. Given an array nums, find a peak element and return its index. If the array contains multiple peaks, return any.

You must write an O(log n) solution.

Example 1:

Input: 1 2 3 1
Output: 2

Example 2:

Input: 1 2 1 3 5 6 4
Output: 5

Constraints:

  • 1 ≤ nums.length ≤ 1000
  • -2³¹ ≤ nums[i] ≤ 2³¹ - 1
  • nums[i] ≠ nums[i+1]

Input format: Space-separated integers.

Output format: Index of any peak.

Run to check your code against the sample cases, or submit to run every case