What if you could find the highest mountain without climbing every peak?
Why Find Peak Element Using Binary Search in DSA C#?
Imagine you have a long list of numbers representing mountain heights along a trail. You want to find a peak, a mountain taller than its neighbors, but checking each mountain one by one would take a lot of time.
Manually checking each mountain means walking the entire trail, comparing every mountain with its neighbors. This is slow and tiring, especially if the trail is very long. You might also make mistakes by missing comparisons or stopping too early.
Using binary search, you can quickly jump to the middle mountain and decide which side to explore next based on height comparisons. This way, you find a peak much faster without checking every mountain.
int FindPeak(int[] heights) {
if (heights.Length > 1 && heights[0] > heights[1])
return 0;
for (int i = 1; i < heights.Length - 1; i++) {
if (heights[i] > heights[i - 1] && heights[i] > heights[i + 1])
return i;
}
return heights.Length - 1;
}int FindPeak(int[] heights) {
int left = 0, right = heights.Length - 1;
while (left < right) {
int mid = (left + right) / 2;
if (heights[mid] > heights[mid + 1]) right = mid;
else left = mid + 1;
}
return left;
}This method lets you find a peak element in a list quickly and efficiently, even if the list is very large.
Finding a local maximum in stock prices to decide the best time to sell without checking every single price point.
Manual search checks every element, which is slow.
Binary search jumps smartly to find a peak faster.
Useful for large data where speed matters.
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
