...

/

Element in Bitonic Array

Element in Bitonic Array

Let's try to conduct a binary search of a bitonic array. Difficulty Level: Medium

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:

  • 3nums.length1053 \leq nums.length \leq 10^5

  • 1nums[i],target1081 \leq nums[i], target \leq 10^8

  • 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 lef ...