0
0
DSA C++programming

Find Peak Element Using Binary Search in DSA C++

Choose your learning style9 modes available
Mental Model
A peak element is one that is bigger than its neighbors. We use binary search to quickly find such an element by checking the middle and deciding which half to explore next.
Analogy: Imagine climbing a mountain range where you want to find a peak. Instead of walking the whole range, you look at the middle point. If the slope goes up to the right, you move right; if it goes up to the left, you move left. This way, you quickly find a peak without checking every step.
Index: 0   1   2   3   4   5   6
Value: 1 -> 3 -> 2 -> 5 -> 4 -> 6 -> 3
          ↑
        mid pointer
Dry Run Walkthrough
Input: array: [1, 3, 2, 5, 4, 6, 3]
Goal: Find any peak element index where element is greater than neighbors
Step 1: Calculate mid index as 3, value is 5
Array: 1 -> 3 -> 2 -> [5] -> 4 -> 6 -> 3
mid=3
Why: Start by checking middle element to decide which half to search next
Step 2: Compare mid value 5 with next element 4; since 5 > 4, move search to left half including mid
Search range: indices 0 to 3
Array: 1 -> 3 -> 2 -> [5]
Why: If mid is greater than next, peak must be on left side or at mid
Step 3: Calculate new mid index as 1, value is 3
Array: 1 -> [3] -> 2 -> 5
mid=1
Why: Check middle of left half to narrow down peak location
Step 4: Compare mid value 3 with next element 2; since 3 > 2, move search to left half including mid
Search range: indices 0 to 1
Array: 1 -> [3]
Why: Peak is on left side or at mid because mid is greater than next
Step 5: Calculate new mid index as 0, value is 1
Array: [1] -> 3
mid=0
Why: Check middle of new range to find peak
Step 6: Compare mid value 1 with next element 3; since 1 < 3, move search to right half excluding mid
Search range: indices 1 to 1
Array: [3]
Why: If mid is less than next, peak must be on right side
Step 7: Only one element left at index 1, which is 3; this is a peak
Peak found at index 1 with value 3
Why: Single element left is peak by definition
Result:
Peak element index: 1, value: 3
Annotated Code
DSA C++
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int findPeakElement(const vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            // Compare mid element with next element
            if (nums[mid] > nums[mid + 1]) {
                // Peak is in left half including mid
                right = mid;
            } else {
                // Peak is in right half excluding mid
                left = mid + 1;
            }
        }
        // left == right is peak index
        return left;
    }
};

int main() {
    Solution sol;
    vector<int> nums = {1, 3, 2, 5, 4, 6, 3};
    int peakIndex = sol.findPeakElement(nums);
    cout << "Peak element index: " << peakIndex << ", value: " << nums[peakIndex] << endl;
    return 0;
}
while (left < right) {
loop until search range narrows to one element
int mid = left + (right - left) / 2;
calculate middle index to check
if (nums[mid] > nums[mid + 1]) {
if mid element is greater than next, peak is on left side including mid
right = mid;
move right boundary to mid to narrow search to left half
else { left = mid + 1; }
otherwise, move left boundary to mid+1 to search right half
return left;
return the index where left meets right, which is peak
OutputSuccess
Peak element index: 1, value: 3
Complexity Analysis
Time: O(log n) because binary search halves the search space each step
Space: O(1) because only a few variables are used regardless of input size
vs Alternative: Compared to linear scan O(n), binary search is much faster for large arrays
Edge Cases
Single element array
Returns index 0 as peak since only one element exists
DSA C++
while (left < right) {
Strictly increasing array
Peak is last element because each next is bigger; binary search moves right
DSA C++
else { left = mid + 1; }
Strictly decreasing array
Peak is first element because each next is smaller; binary search moves left
DSA C++
if (nums[mid] > nums[mid + 1]) { right = mid; }
When to Use This Pattern
When you need to find a local maximum or peak in an array efficiently, use binary search by comparing mid element with its neighbor to decide search direction.
Common Mistakes
Mistake: Comparing mid element with previous instead of next element
Fix: Always compare mid with next element to decide which half contains a peak
Mistake: Using while (left <= right) causing infinite loop
Fix: Use while (left < right) to ensure loop ends when left meets right
Summary
Finds an index of a peak element where the element is greater than its neighbors.
Use when you want to quickly find a local maximum without checking every element.
The key insight is comparing mid element with its next neighbor to decide which half to search next.