Find Peak Element Using Binary Search in DSA C++ - Time & Space Complexity
We want to know how fast the binary search method finds a peak element in an array.
How does the number of steps grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
int findPeakElement(std::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]) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
This code finds a peak element by repeatedly dividing the search space in half using binary search.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that halves the search range each time.
- How many times: About log base 2 of n times, where n is the array size.
Each step cuts the array size roughly in half, so the number of steps grows slowly as the array grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the input size adds only one extra step.
Time Complexity: O(log n)
This means the steps needed grow very slowly even if the array gets much bigger.
[X] Wrong: "The loop runs n times because it looks like it checks each element."
[OK] Correct: Actually, the loop cuts the search space in half each time, so it runs much fewer times than n.
Understanding this binary search approach shows you can find answers quickly by smartly reducing work, a key skill in many coding challenges.
"What if we changed the array to be sorted strictly increasing or strictly decreasing? How would the time complexity change?"
Practice
Solution
Step 1: Understand the definition of a peak element
A peak element is one that is strictly greater than its neighbors on both sides (or only one side if at the boundary).Step 2: Apply this to binary search context
Binary search finds such an element by comparing mid element with neighbors to decide search direction.Final Answer:
An element that is greater than its neighbors -> Option AQuick Check:
Peak element = greater than neighbors [OK]
- Confusing peak with smallest element
- Thinking peak is always first or last element
- Ignoring boundary conditions
Solution
Step 1: Identify valid index range for array of size n
Array indices go from 0 to n-1, so left should start at 0 and right at n-1.Step 2: Check each option for correct initialization
int left = 0, right = n - 1; correctly initializes left=0 and right=n-1, suitable for binary search.Final Answer:
int left = 0, right = n - 1; -> Option CQuick Check:
Index range = 0 to n-1 [OK]
- Starting left at 1 instead of 0
- Setting right to n instead of n-1
- Confusing inclusive and exclusive ranges
[1, 3, 20, 4, 1, 0] using binary search peak finding?Solution
Step 1: Identify peak elements in the array
Array: [1, 3, 20, 4, 1, 0]. 20 at index 2 is greater than neighbors 3 and 4, so it's a peak.Step 2: Binary search will find index 2 as peak
Binary search compares mid elements and moves left or right until it finds 20 at index 2.Final Answer:
2 -> Option BQuick Check:
Peak at index 2 = 20 [OK]
- Choosing first or last element as peak incorrectly
- Confusing index with value
- Not checking both neighbors
int findPeak(int arr[], int n) {
int left = 0, right = n - 1;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] < arr[mid + 1])
left = mid + 1;
else
right = mid - 1;
}
return left;
}Solution
Step 1: Analyze the binary search update steps
If arr[mid] >= arr[mid+1], the code sets right = mid - 1, which skips mid as a candidate peak.Step 2: Correct approach is to set right = mid to include mid in search
Setting right = mid keeps mid in range; subtracting 1 may miss the peak.Final Answer:
Decreasing right by 1 skips potential peak -> Option AQuick Check:
Right update must keep mid included [OK]
- Setting right = mid - 1 instead of right = mid
- Ignoring mid element as possible peak
- Incorrect loop condition
[10, 20, 15, 2, 23, 90, 67]. Using binary search for peak element, which index will be returned and why?Solution
Step 1: Identify all peak elements in the array
Array: [10, 20, 15, 2, 23, 90, 67]. Peaks are 20 (index 1) and 90 (index 5). 90 is greater than neighbors 23 and 67.Step 2: Binary search tends to find a peak, often the higher one
Binary search will find 90 at index 5 because it is a higher peak than 20 at index 1.Final Answer:
5, because 90 is a peak element -> Option DQuick Check:
Highest peak found = index 5 [OK]
- Assuming first peak is always returned
- Ignoring multiple peaks in array
- Confusing last element as peak without checking neighbors
Ready to prove it?
Pass the test and this pattern moves unknown
