Complete the code to initialize the middle index for binary search.
let mid = Math.[1]((left + right) / 2);
We use Math.floor to get the middle index as an integer by rounding down.
Complete the condition to check if mid element is a peak.
if ((mid === 0 || nums[mid] > nums[mid - 1]) && (mid === nums.length - 1 || nums[mid] > nums[[1]])) {
We compare the mid element with the next element at mid + 1 to check if it is greater.
Fix the error in updating the left pointer when mid element is less than the next element.
if (nums[mid] < nums[mid + 1]) { left = [1]; } else { right = mid; }
We move left to mid + 1 to search the right half where a peak may exist.
Fill both blanks to complete the binary search loop condition and return statement.
while ([1] < [2]) { // binary search steps } return left;
The loop runs while left < right. After the loop, left is the peak index.
Fill all three blanks to complete the function signature, initialize pointers, and return the peak index.
function findPeakElement([1]: number[]): number { let [2] = 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; } } return [3]; }
The function takes nums array, initializes left pointer, and returns left as the peak index.