Try the Question
Before reading the explanation, try solving the problem yourself:
👉 https://leetcode.com/problems/find-peak-element/
Attempting the problem first helps develop algorithmic intuition, especially for binary search problems.
Problem Statement
You are given a 0-indexed integer array nums.
Your task is to find the index of a peak element.
What is a Peak Element?
A peak element is an element that is strictly greater than its immediate neighbors.
For an element nums[i] to be a peak:
However, the array boundaries have a special rule.
Boundary Rule
You can imagine that:
This means:
- The first element only needs to be greater than the second element.
- The last element only needs to be greater than the second last element.
Constraints
Important implications:
- Adjacent elements are never equal.
- The array always has at least one peak.
Understanding the Problem with Examples
Example 1
Input
Visualization
Here:
So 3 is a peak element.
Output
(index of value 3)
Example 2
Input
Visualization
Possible peaks:
Valid outputs:
Either peak index is acceptable.
Key Observations
Important facts about peak elements:
- Every array must contain at least one peak.
- If numbers keep increasing, the last element will be a peak.
- If numbers keep decreasing, the first element will be a peak.
- If the array has ups and downs, peaks exist in the middle.
This guarantees a solution exists.
Approach 1 — Linear Scan (Brute Force)
The simplest approach is to check each element and see whether it satisfies the peak condition.
Algorithm
- Traverse the array.
- Check if the current element is greater than neighbors.
- Return the index if found.
Example
Check:
Implementation
Time Complexity
Space Complexity
Although simple, this solution does not satisfy the required O(log n) complexity.
Approach 2 — Binary Search Using Peak Conditions
Instead of scanning the entire array, we can use binary search.
Key Idea
If we are at index mid, compare it with the neighbors.
Three situations may occur.
Case 1 — Peak Found
Then:
Case 2 — Increasing Slope
Example:
This means the peak must exist on the right side.
Move search to the right.
Case 3 — Decreasing Slope
Example:
Peak exists on the left side.
Move search to the left.
Implementation
Time Complexity
Space Complexity
Approach 3 — Optimized Binary Search (Most Elegant Solution)
A simpler and more elegant version of binary search exists.
Core Intuition
Compare:
Two possibilities exist.
Case 1 — Increasing Slope
Example:
The peak must exist on the right side.
Move:
Case 2 — Decreasing Slope
Example:
Peak exists on the left side including mid.
Move:
This gradually narrows down to the peak.
Final Optimized Implementation
Step-by-Step Example
Array
Iteration 1
Increasing slope → move right.
Iteration 2
Decreasing slope → move left.
Eventually:
Value:
Why This Binary Search Works
The array behaves like a mountain landscape.
Example visualization:
If you stand at mid:
- If the right side is higher, go right.
- If the left side is higher, go left.
This guarantees that you will eventually reach a peak.
Time Complexity
Each iteration cuts the search space in half.
Space Complexity
Only a few variables are used.
Key Takeaways
✔ A peak element is greater than its neighbors
✔ The array always contains at least one peak
✔ Binary search can be applied using slope comparison
✔ The optimized solution only compares nums[mid] and nums[mid+1]
✔ The final algorithm runs in O(log n) time
Final Thoughts
This problem is an excellent example of applying binary search beyond sorted arrays.
Instead of searching for a value, binary search is used here to locate a structural property of the array (a peak).
Understanding this pattern is extremely valuable because similar logic appears in many interview problems involving:
- Mountain arrays
- Bitonic arrays
- Optimization search problems
Mastering this approach will significantly improve your binary search problem-solving skills for technical interviews.




