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.
How does the number of steps grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
public int FindPeakElement(int[] nums) {
int left = 0, right = nums.Length - 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 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 n times, where n is the array length.
Each step cuts the search area 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 number of steps grows slowly, making the search very efficient even for large arrays.
[X] Wrong: "The algorithm checks every element, so it must be O(n)."
[OK] Correct: The algorithm 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 the array was sorted in descending order? 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 CQuick Check:
Peak element = greater than neighbors [OK]
- Confusing peak with minimum element
- Thinking peak must be at array ends
- Assuming peak equals maximum element in array
Solution
Step 1: Understand overflow risk in mid calculation
Using (low + high) / 2 can overflow if low and high are large integers.Step 2: Use safe calculation 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
- Mixing up low and high in formula
[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]. 20 at index 2 is greater than neighbors 3 and 4.Step 2: Binary search finds peak at index 2
Binary search compares mid elements and narrows down to index 2 as peak.Final Answer:
2 -> Option BQuick Check:
Peak element index = 2 [OK]
- Choosing first or last element incorrectly
- Confusing peak with global max
- Not checking both neighbors
int mid = low + (high - low) / 2;
if (arr[mid] < arr[mid + 1])
low = mid + 1;
else
high = mid - 1;Solution
Step 1: Check boundary conditions for arr[mid + 1]
If mid is the last index, arr[mid + 1] causes index out of range error.Step 2: Need to ensure mid + 1 is within array bounds
Code must check or limit mid to avoid accessing beyond array length.Final Answer:
It may cause index out of range when mid is at the last element -> Option AQuick Check:
Check array bounds before accessing mid+1 [OK]
- Ignoring array boundary causing runtime errors
- Incorrect mid calculation causing overflow
- Wrong comparison direction in if condition
[0, 2, 4, 7, 5, 3, 1], which modification to the binary search algorithm helps find the peak element efficiently?Solution
Step 1: Understand mountain array property
Mountain array increases then decreases, so peak is where arr[mid] > arr[mid+1].Step 2: Use binary search comparing mid and mid+1
If arr[mid] < arr[mid+1], move low to mid+1; else move high to mid to find peak.Final Answer:
Compare mid with mid+1 and move low or high accordingly -> Option AQuick Check:
Compare mid and mid+1 to find peak [OK]
- Moving pointers incorrectly ignoring array shape
- Using linear search losing binary search efficiency
- Comparing mid with mid-1 incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
