Complete the code to initialize the search boundaries for binary search.
left, right = 0, [1]
We set right to len(nums) - 1 because list indices go from 0 to length minus one.
Complete the code to find the middle index in binary search.
mid = left [1] (right - left) // 2
We add half the distance between left and right to left to find the middle index.
Fix the error in the condition to decide which half to search next.
if nums[mid] < nums[[1]]:
We compare with the element right after mid, so use mid + 1.
Fill both blanks to update the search boundaries correctly.
if nums[mid] < nums[mid + 1]: [1] = mid + 1 else: [2] = mid
If the middle element is less than its right neighbor, move left to mid + 1 to search right half; else move right to mid to search left half.
Fill all three blanks to return the peak element index after binary search.
def findPeakElement(nums): left, right = 0, len(nums) - 1 while left < right: mid = left + (right - left) // 2 if nums[mid] < nums[mid + 1]: left = mid + 1 else: right = mid return [1] # Peak index peak_index = findPeakElement([2]) print(peak_index, nums[[3]])
The peak index is where left and right meet, so return left. We call the function with nums and print the value at peak_index.
