#162
Medium Algorithms Find peak element
Array Binary Search
46.8% acceptance
Jan 12, 2026
14475
4941
A peak element is an element that is strictly greater than its neighbors.
Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.
You must write an algorithm that runs in O(log n) time.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn find_peak_element(nums: Vec<i32>) -> i32 {
let mut left = 0;
let mut right = nums.len() - 1;
while left < right {
let mid = left + (right - left) / 2;
if nums[mid] > nums[mid + 1] {
right = mid;
} else {
left = mid + 1;
}
}
left as i32
}
}