Introduction
The Find Peak Element in an Array problem is a very popular DSA interview question that looks simple but tests your ability to apply binary search in a non-obvious way.
In simple words, this problem asks you to find an element that is greater than its neighbors.
This article explains the problem in simple, human-friendly language, with real-life meaning, clear logic, and a binary search approach that interviewers expect.
Real-World Meaning of Peak Element
Imagine you are driving on a hilly road:
The road goes up
Then at some point it goes down
The highest point you cross is a peak.
In the same way, a peak element in an array is a number that is bigger than the numbers next to it.
What is a Peak Element?
An element is called a peak element if:
It is greater than its left neighbor
It is greater than its right neighbor
Important Notes
The array does not need to be sorted
There can be multiple peak elements
You can return any one peak
Problem Statement
You are given an array of integers. Find the index of any peak element.
Example
Array: [1, 2, 3, 1]
Output: 2
Explanation:
3is greater than2and1, so it is a peak
Before vs After Understanding
Brute Force Thinking (Before)
Check every element
Compare with neighbors
Time-consuming for large arrays
Binary Search Thinking (After)
Look at middle element
Decide which side has a peak
Reduce search space by half
Why Binary Search Works Here
This may feel confusing at first because the array is not sorted.
Binary search works because:
If one side is going up, a peak must exist there
If one side is going down, a peak exists on the other side
This guarantees a peak in the chosen half.
What Interviewers Are Actually Testing
Interviewers want to see:
Can you apply binary search creatively?
Do you understand problem properties?
Can you think beyond sorted arrays?
This problem checks problem-solving depth, not memorization.
Key Idea (Very Simple)
Compare middle element with its right neighbor
If middle is smaller, peak lies on the right
If middle is bigger, peak lies on the left or is the middle itself
Step-by-Step Logic
Set
low = 0,high = n - 1Find
midCompare
arr[mid]witharr[mid + 1]If
arr[mid] < arr[mid + 1], move rightElse, move left
Continue until
low == high
That index is a peak.
Dry Run Example
Array: [1, 2, 3, 1]
| low | high | mid | comparison | move |
|---|---|---|---|---|
| 0 | 3 | 1 | 2 < 3 | right |
| 2 | 3 | 2 | 3 > 1 | left |
Answer index = 2
One-Line Logic Before Code
Move toward the side where values are increasing.
Code Implementation (C++)
int findPeakElement(vector<int>& nums) {
int low = 0, high = nums.size() - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (nums[mid] < nums[mid + 1])
low = mid + 1;
else
high = mid;
}
return low;
}
Common Beginner Mistakes
Thinking array must be sorted
Checking both neighbors unnecessarily
Overcomplicating the logic
Time and Space Complexity
Time Complexity: O(log n)
Space Complexity: O(1)
This is much faster than brute force.
Easy Summary (Explain Like I’m 10)
If numbers go up and then go down, the highest number in between is a peak. By always moving in the direction where numbers are rising, we can quickly find a peak without checking every number.
Summary
Finding a peak element in an array using binary search is a smart application of binary search beyond sorted data. By comparing middle elements with their neighbors and reducing the search space, you can find a peak efficiently in logarithmic time. This problem is commonly asked in interviews to test logical thinking and binary search mastery.
Join the conversation! Your thoughts help the community grow.