Asked at

Find Peak Element

Medium
Verified
ArrayBinary Search~20 min

A peak element is strictly greater than its neighbors. Given nums, return the index of any peak. Treat out-of-bounds neighbors as -∞, so the ends can be peaks.

Run in O(log n) time. The input arrives as a single array nums. Any valid peak index is accepted.

Examples

in[1, 2, 3, 1]
out2

nums[2] == 3 is greater than both neighbors.

in[1, 2, 1, 3, 5, 6, 4]
out5

nums[5] == 6 is a peak; index 1 would also be valid.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -2³¹ ≤ nums[i] ≤ 2³¹ - 1
  • nums[i] != nums[i + 1] for all valid i.
  • Target: O(log n) time.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.