0
0
DSA C++programming~20 mins

Find Peak Element Using Binary Search in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Peak Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find Peak Element Output
What is the output index of the peak element for the given array using the binary search approach?
DSA C++
int findPeakElement(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[mid + 1]) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}

vector<int> nums = {1, 3, 20, 4, 1, 0};
int peakIndex = findPeakElement(nums);
cout << peakIndex;
A3
B1
C2
D0
Attempts:
2 left
💡 Hint
Check where the element is greater than its next neighbor.
Predict Output
intermediate
2:00remaining
Find Peak Element Output with Multiple Peaks
What is the output index of the peak element for the array with multiple peaks using the binary search approach?
DSA C++
int findPeakElement(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[mid + 1]) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}

vector<int> nums = {1, 2, 3, 1, 2, 1};
int peakIndex = findPeakElement(nums);
cout << peakIndex;
A2
B4
C1
D3
Attempts:
2 left
💡 Hint
Binary search finds any peak, not necessarily the first or last.
🧠 Conceptual
advanced
2:00remaining
Why Binary Search Works for Peak Element?
Why does the binary search approach always find a peak element in an unsorted array?
ABecause if the middle element is smaller than the next, a peak must exist on the right side; otherwise, on the left side.
BBecause the array is sorted, so the middle element is always a peak.
CBecause the algorithm checks all elements one by one to find the peak.
DBecause the peak element is always at the start or end of the array.
Attempts:
2 left
💡 Hint
Think about the direction where the array is increasing or decreasing.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Peak Element Code
What error will this code cause when finding a peak element?
DSA C++
int findPeakElement(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[mid + 1]) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return left;
}
ACompiles but returns -1 always
BRuntime error due to out-of-bounds access at nums[mid + 1]
CReturns wrong peak index because of incorrect mid calculation
DInfinite loop because left and right pointers never converge
Attempts:
2 left
💡 Hint
Check the condition when mid is at the last index.
🚀 Application
expert
3:00remaining
Find Peak Element in Circular Array
Given a circular array where the end connects to the start, which approach correctly finds a peak element?
ASort the array first then pick the middle element as peak
BLinear scan from start to end to find any element greater than neighbors
CUse binary search but ignore wrap-around and compare only nums[mid] and nums[mid + 1]
DUse the standard binary search comparing nums[mid] and nums[(mid + 1) % n] to handle wrap-around
Attempts:
2 left
💡 Hint
Think about how to handle the circular nature in index calculation.