Find Peak Element
Problem Statement
A peak is an element that is strictly bigger than both of its neighbours.
You are given an array nums. Return the index of any peak β if there are several, any one is accepted.
Pretend there is a value of ββ just before the array and just after it (nums[β1] = nums[n] = ββ). So the first element only has to beat its right neighbour, and the last only has to beat its left neighbour. Two neighbours are never equal.
Your solution must run in O(log n) time.
An integer array nums where nums[i] != nums[i + 1] for every valid i.
The index of any peak element.
3 is bigger than 2 on its left and 1 on its right.
Index 1 (value 2) is a peak, and index 5 (value 6) is a peak. Returning either one is correct.
The values only climb. The last element 4 beats 3, and after it comes ββ, so it is a peak.
- β’ 1 β€ n β€ 1000
- β’ β2^31 β€ nums[i] β€ 2^31 β 1
- β’ nums[i] != nums[i + 1] for all valid i
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Scan left to right; the first i with nums[i] > nums[i+1] is a peak
If nums[mid] < nums[mid+1], a peak is to the right β lo = mid + 1; else hi = mid
Walk from the left. Since nums[β1] = ββ, the first element beats its left side. As long as the next value is bigger, keep walking. The first time the next value is smaller, you are on a peak. If that never happens, the last element is a peak.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.