
Leetcode Problem 162: Find Peak Element
A peak element is an element that is strictly greater than its immediate neighbors. Given a 0-indexed integer array nums, find any peak element and return its index. The array boundaries behave as negative infinity — the first and last elements only need to be greater than their single neighbour to qualify as a peak.
Your solution must run in O(log n) time.
Constraints:
function findPeakElement(nums: number[]): number {
let left = 0;
let right = nums.length - 1;
while (left < right) {
let mid = left + Math.floor((right - left) / 2);
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
console.log(findPeakElement([1,2,1,3,5,6,4]))