Mastery Points
0
Find Peak Element in dsa
Context & Logic
A peak element is an element that is strictly greater than its neighbors.
Example
function findPeak(nums) {
let low = 0, high = nums.length - 1;
while (low < high) {
let mid = Math.floor((low + high) / 2);
if (nums[mid] > nums[mid + 1]) high = mid;
else low = mid + 1;
}
return low;
}Step-by-Step Logic
1
Compare the middle element with its next neighbor.
2
Move towards the direction of the larger element.
Complexity Metrics
Time Efficiency
O(log n)
Memory Footprint
O(1)