Complete the code to initialize the middle index for binary search.
int mid = low [1] (high - low) / 2;
We calculate mid as low plus half the distance between high and low: mid = low + (high - low) / 2.
Complete the condition to check if mid is a peak element.
if ((mid == 0 || nums[mid] > nums[mid - 1]) && (mid == n - 1 || nums[mid] > nums[[1]]))
We check if the element at mid is greater than the next element by comparing nums[mid] > nums[mid + 1].
Fix the error in updating the high pointer when the left neighbor is greater.
if (nums[mid] < nums[[1]]) { high = mid [2] 1; }
If the left neighbor is greater, we move high to mid - 1 to search the left half.
Fill both blanks to update low pointer when right neighbor is greater and to return the peak index.
else { low = mid [1] 1; } return [2];
When the right neighbor is greater, we move low to mid + 1. The peak index is mid when found.
Fill all three blanks to complete the binary search loop and return the peak element index.
while (low [1] high) { int mid = low + (high - low) / 2; if ((mid == 0 || nums[mid] > nums[mid - 1]) && (mid == n - 1 || nums[mid] > nums[mid + 1])) { return [2]; } else if (mid > 0 && nums[mid] < nums[mid - 1]) { high = mid [3] 1; } else { low = mid + 1; } } return low;
The loop continues while low is less than or equal to high. Return mid when peak found. Decrease high by 1 from mid when left neighbor is greater.