DSA · Binary Search · #74
Find Peak Element
Module 59 · difficulty 3/5·⏱ 30:00starts on first keystroke
A **peak element** is an element that is strictly greater than its neighbors. Given a 0-indexed integer array `nums`, implement `findPeakElement(nums)` that returns the index of **any** peak element. You may imagine that `nums[-1] = nums[n] = -∞` (negative infinity). In other words, an element is always considered strictly greater than a neighbor that is outside the array. You must write an algorithm that runs in **O(log n)** time.
Examples
nums = [1,2,3,1]→2— nums[2] = 3 is a peak; its neighbors 2 and 1 are both smaller.nums = [1,2,1,3,5,6,4]→5— Index 1 (value 2) or index 5 (value 6) are both valid peaks; returning either is accepted.nums = [1]→0— A single element is a peak (both neighbors are -∞).
Constraints
- · 1 <= nums.length <= 1000
- · -2^31 <= nums[i] <= 2^31 - 1
- · nums[i] != nums[i + 1] for all valid i
- · Algorithm must run in O(log n) time
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.