What if you could find the highest hill without climbing them all?
Why Find Peak Element Using Binary Search in DSA Typescript?
Imagine you have a long row of hills, and you want to find the highest hill without climbing every single one.
Checking each hill one by one takes a lot of time and effort.
Going hill by hill is slow and tiring.
You might miss the highest hill if you get tired or lose focus.
It's like searching for a needle in a haystack by looking at every straw.
Using binary search, you can quickly jump to the middle hill and decide which side to explore next.
This way, you skip many hills and find a peak much faster.
function findPeakElement(nums: number[]): number {
for (let i = 0; i < nums.length; i++) {
if ((i === 0 || nums[i] > nums[i - 1]) && (i === nums.length - 1 || nums[i] > nums[i + 1])) {
return i;
}
}
return -1;
}function findPeakElement(nums: number[]): number {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[mid + 1]) right = mid;
else left = mid + 1;
}
return left;
}This method lets you find a peak quickly even in very large lists, saving time and effort.
Finding the highest point in a mountain range using fewer steps, or locating a local maximum in stock prices efficiently.
Manual search checks every element, which is slow.
Binary search jumps smartly to find a peak faster.
This approach works well for large data sets.
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 which half to search next.Final Answer:
An element that is greater than its neighbors -> Option AQuick Check:
Peak element = greater than neighbors [OK]
- Thinking peak is smallest element
- Confusing peak with first or last element always
- Assuming peak equals neighbors
nums?Solution
Step 1: Identify the loop type for binary search
Binary search uses a while loop with condition left < right to narrow down the search range.Step 2: Check options for correct binary search syntax
while(left < right) { ... } shows the correct while loop structure for binary search.Final Answer:
while(left < right) { ... } -> Option AQuick Check:
Binary search uses while loop [OK]
- Using for loop instead of while
- Using if instead of loop for search
- Using switch incorrectly for binary search
nums = [1, 3, 20, 4, 1, 0] using binary search peak finding?Solution
Step 1: Identify peak elements in the array
Check neighbors: 20 at index 2 is greater than 3 and 4, so it's a peak.Step 2: Binary search will find index 2 as peak
Binary search compares mid with neighbors and narrows to index 2.Final Answer:
2 -> Option CQuick Check:
Peak at index 2 = 20 [OK]
- Choosing first or last element incorrectly
- Confusing peak with global max always
- Ignoring neighbors in peak definition
let left = 0;
let right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;Solution
Step 1: Analyze the else branch update of right pointer
Setting right = mid - 1 skips the mid element, possibly missing peak.Step 2: Correct approach is right = mid to keep mid in range
To ensure peak is not skipped, right should be set to mid, not mid - 1.Final Answer:
The else branch should set right = mid, not mid - 1 -> Option DQuick Check:
Keep mid in range by right = mid [OK]
- Setting right = mid - 1 causing missed peaks
- Changing loop condition unnecessarily
- Miscomputing mid index
nums = [10, 20, 15, 2, 23, 90, 67]. Using binary search to find a peak element, which index could be returned as a valid peak?Solution
Step 1: Identify all peak elements in the array
Peaks are elements greater than neighbors: 20 at index 1 and 90 at index 5.Step 2: Binary search can return any peak; index 5 (value 90) is a valid peak
90 is greater than 23 and 67, so index 5 is a valid peak.Final Answer:
5 -> Option BQuick Check:
90 at index 5 is peak [OK]
- Assuming only one peak exists
- Choosing non-peak elements
- Ignoring neighbors when checking peak
Ready to prove it?
Pass the test and this pattern moves unknown
