Search⌘ K
AI Features

Solution: Find Peak Element

Explore how to identify a peak element in an array by applying modified binary search to navigate hills and valleys. Learn to efficiently narrow down the search space by comparing middle elements, ensuring a solution that runs in logarithmic time complexity and constant space. This lesson helps you master a common coding interview pattern for peak element finding problems.

Statement

You’re given a 0-indexed integer array nums. An index i is called a peak if nums[i] is strictly greater than its neighboring values (the elements immediately to its left and right, if they exist). Assume the array has virtual boundaries where nums[-1] = nums[n] = -∞, so the first and last elements can also be peaks.

Your task is to return the index of any one peak element (if there are multiple peaks, any valid peak index is acceptable), and your solution must run in O(logn)O(\log n) time.

Constraints:

  • 1 ...