Repair a Binary Search
Problem statement
You are given an integer array values sorted in non-decreasing order and an integer target.
Return the last index at which target occurs. If the value is absent, return -1.
Your implementation must use binary search and must terminate for adjacent search boundaries and duplicate values.
Function
repairedBinarySearch(values: int[], target: int) → intExamples
Example 1
values = [1,2,5,9,9]target = 5return = 2The value 5 occurs at index 2.
Example 2
values = [1,2,5,9,9]target = 9return = 4The target occurs at indices 3 and 4, so the last occurrence is index 4.
Example 3
values = []target = 7return = -1An empty array contains no matching occurrence.
Constraints
0 <= values.length <= 100000-1000000000 <= values[i], target <= 1000000000valuesis sorted in non-decreasing order.