Search⌘ K
AI Features

Problem: Find Peak Element

Explore how to use binary search to find a peak element in an integer array. This lesson guides you through implementing a logarithmic time algorithm that compares middle elements with neighbors to locate a peak efficiently. Understand the logic and complexity behind this approach to improve your problem-solving skills in searching algorithms.

Statement

A peak element in an array is one that is strictly greater than both of its neighbors.

Given a 00-indexed integer array nums, return the index of any peak element.

Note: Elements at the boundaries of the array are compared against -\infty. That is, nums[-1] and nums[n] are both treated as -\infty, where n is the length of nums. This means an element can be considered a peak if it only needs to be greater than its single existing neighbor at the edges. Additionally, no two adjacent elements in nums are equal. You must write an algorithm that runs in O(logn)O(\log n) time.

...