Find Peak Element Using Binary Search in DSA C - Time & Space Complexity
We want to understand how fast the binary search method finds a peak element in an array.
The question is: how does the time taken grow as the array size grows?
Analyze the time complexity of the following code snippet.
int findPeakElement(int* nums, int numsSize) {
int left = 0, right = numsSize - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
This code finds a peak element index in the array by repeatedly narrowing the search range 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: The loop runs about log base 2 of the array size times.
Each step cuts the search area roughly in half, so the number of steps grows slowly as the array gets bigger.
| 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 more step, showing slow growth.
Time Complexity: O(log n)
This means the time to find a peak grows slowly, increasing by one step each time the input size doubles.
[X] Wrong: "The algorithm checks every element, so it must be O(n)."
[OK] Correct: The code does not check every element; it smartly halves the search space each time, so it runs much faster than checking all elements.
Understanding this binary search approach shows you can use smart searching to solve problems efficiently, a skill valued 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 just 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 the search direction.Final Answer:
An element that is greater than its neighbors -> Option AQuick Check:
Peak element = greater than neighbors [OK]
- Confusing peak with minimum element
- Thinking peak must be at array ends only
- Assuming peak equals neighbors
Solution
Step 1: Understand integer overflow risk in mid calculation
Using (low + high) / 2 can overflow if low and high are large integers.Step 2: Use safer formula to avoid overflow
Calculate mid as low + (high - low) / 2 to prevent overflow.Final Answer:
int mid = low + (high - low) / 2; -> Option DQuick Check:
Safe mid calculation = low + (high - low) / 2 [OK]
- Using (low + high)/2 causing overflow
- Subtracting incorrectly in mid calculation
- Multiplying instead of dividing
[1, 3, 20, 4, 1, 0] using binary search peak finding?Solution
Step 1: Identify peak candidates in the array
Array: [1, 3, 20, 4, 1, 0]. Element 20 at index 2 is greater than neighbors 3 and 4.Step 2: Binary search will find index 2 as peak
Binary search compares mid elements and moves towards the peak, landing on index 2.Final Answer:
2 -> Option BQuick Check:
Peak element index = 2 [OK]
- Choosing first or last element incorrectly
- Confusing index with value
- Ignoring neighbors in peak definition
int findPeak(int arr[], int n) {
int low = 0, high = n - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] < arr[mid + 1])
low = mid + 1;
else
high = mid - 1;
}
return low;
}
What is the bug in this code?Solution
Step 1: Analyze the else branch update of high
Setting high = mid - 1 skips the mid element which could be a peak.Step 2: Correct update to include mid in search
Change to high = mid to keep mid in the search space for peak.Final Answer:
The else branch should set high = mid, not mid - 1 -> Option CQuick Check:
Keep mid in search by high = mid [OK]
- Reducing high too early and missing peak
- Changing while condition unnecessarily
- Ignoring mid element in binary search
[10, 20, 15, 2, 23, 90, 67], how many peak elements can binary search find and which index will it return if it always moves towards the right when equal?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) as both are greater than neighbors.Step 2: Understand binary search behavior moving right on equal
Binary search will move towards the right peak at index 5 because it moves right when equal or increasing.Final Answer:
2 peaks, returns index 5 -> Option AQuick Check:
Two peaks, rightmost peak index 5 [OK]
- Assuming only one peak always
- Ignoring binary search direction rules
- Confusing peak count with returned index
Ready to prove it?
Pass the test and this pattern moves unknown
