Find Peak Element Problem
Find Peak Element Problem — ExecCode Easy DSA Practice
Solve the Find Peak Element problem on ExecCode. Free online easy DSA practice in Binary Search. Write and run code in Java, C++, Python — no signup required to run.
Problem description
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 them. You may imagine that nums[-1] = nums[n] = -infinity, so a boundary element is always considered greater than a neighbor outside the array. You must write an algorithm that runs in O(log n) time.
Examples
Input nums = [1, 2, 3, 1, 5, 6, 4, 3, 2]; Output 5. Input nums = [1, 2, 3, 4, 5]; Output 4. Input nums = [5, 4, 3, 2, 1]; Output 0
Constraints
1 ≤ nums.length ≤ 1000 -2³¹ ≤ nums[i] ≤ 2³¹ - 1 nums[i] ≠ nums[i + 1] for all valid i
Practice Find Peak Element free on ExecCode. Browse DSA problems, topic map, and placement guides.