Solution Review: Problem Challenge 1
We'll cover the following
Search Bitonic Array (medium) #
Given a Bitonic array, find if a given ‘key’ is present in it. An array is considered bitonic if it is monotonically increasing and then monotonically decreasing. Monotonically increasing or decreasing means that for any index i
in the array arr[i] != arr[i+1]
.
Write a function to return the index of the ‘key’. If the ‘key’ is not present, return -1.
Example 1:
Input: [1, 3, 8, 4, 3], key=4
Output: 3
Example 2:
Input: [3, 8, 3, 1], key=8
Output: 1
Example 3:
Input: [1, 3, 8, 12], key=12
Output: 3
Example 4:
Input: [10, 9, 8], key=10
Output: 0
Solution #
The problem follows the Binary Search pattern. Since Binary Search helps us efficiently find a number in a sorted array we can use a modified version of the Binary Search to find the ‘key’ in the bitonic array.
Here is how we can search in a bitonic array:
- First, we can find the index of the maximum value of the bitonic array, similar to Bitonic Array Maximum. Let’s call the index of the maximum number
maxIndex
. - Now, we can break the array into two sub-arrays:
- Array from index ‘0’ to
maxIndex
, sorted in ascending order. - Array from index
maxIndex+1
toarray_length-1
, sorted in descending order.
- Array from index ‘0’ to
- We can then call Binary Search separately in these two arrays to search the ‘key’. We can use the same Order-agnostic Binary Search for searching.
Code #
Here is what our algorithm will look like: