Given a bitonic array arr and an integer target, return the index of target in arr. If target does not exist in the array, return -1.
A bitonic array is first strictly increasing and then strictly decreasing. It contains a single peak element, and the elements on each side of the peak are strictly monotonic. An array that is only increasing or only decreasing is also considered bitonic.
If target appears more than once in the array, return the index of its first occurrence (the smallest index).
Function Signature
def search_bitonic(arr: list[int], target: int) -> int:
Rules
-
Indices are 0-based.
-
A value can appear once before the peak and once after it. In that case, return the smaller index.
Constraints
-
1 <= len(arr) <= 10^4
-
-10^9 <= arr[i], target <= 10^9
-
arr
is a valid bitonic array (strictly increasing, then strictly decreasing).
Examples
Input: arr = [1, 3, 8, 12, 4, 2], target = 4
Output: 4
The array rises to its peak 12 at index 3 and then falls. The value 4 is at index 4, on the falling side.
Input: arr = [10, 9, 8], target = 5
Output: -1
This array is strictly decreasing, which counts as bitonic. The value 5 does not appear, so the answer is -1.
Input: arr = [1, 3, 8, 12, 4, 3], target = 3
Output: 1
The value 3 appears at index 1 on the rising side and at index 5 on the falling side. The first occurrence is index 1.