Back to DSA

Find Peak Element

medium
Acceptance: 47%
Binary Search

An element in an array is a peak if it exceeds both its immediate neighbors. Given an integer array, locate any one peak element and return its index. Treat positions outside the array as negative infinity. Your algorithm must operate in O(log n) time.

Examples

Example 1:
Input:nums = [1,3,2,5,4]
Output:1
Explanation: Index 1 holds value 3, which is greater than neighbors 1 and 2. Index 3 (value 5) is also valid.
Example 2:
Input:nums = [10,9,8,7]
Output:0
Explanation: The first element 10 is a peak because the left boundary is treated as negative infinity.

Hints

00:00
1234567