Element in Bitonic Array
Let's try to conduct a binary search of a bitonic array. Difficulty Level: Medium
We'll cover the following...
Problem statement
Suppose we are given an integer array, nums
, which is guaranteed to be a bitonic array and a target value. It will return the index if the target is found. If not, it will return -1
.
Constraints:
-
-
-
A given array always contains a bitonic point.
-
Array
nums
always contain distinct elements.
Example 1:
Input: [3, 9, 10, 20, 17, 5, 1], 20
Output: 3
Example 1:
Input: [5, 6, 7, 8, 9, 10, 3, 2, 1], 30
Output: -1
Intuition
- Use the
findBitonicPoint
function from the previous lesson to find the index of a bitonic point. Then, we will know that all of the elements on the left hand of the bitonic point are arranged in ascending order and all those on the right hand are sorted in descending order. - Run the binary search algorithm for the ascending and descending parts, separately.
Algorithm for the BS in the ascending array
-
Set the ...