Suppose we have a bitonic sequence, we have to find the Bitonic Point in it. As we know a Bitonic Sequence is a sequence of numbers which is first strictly increasing then after a certain point it is strictly decreasing. This point is bitonic point. For only increasing or only decreasing sequences, bitonic points are not available.
So, if the input is like [7, 8, 9, 12, 10, 6, 3, 2], then the output will be 12
To solve this, we will follow these steps −
- define a function binary_search(array, l, r)
- if l <= r, then −
- m := (l + r) / /2
- if array[m - 1] < array[m] and array[m] > array[m + 1], then −
- return m
- if array[m] < array[m + 1], then −
- return binary_search(array, m + 1, r)
- Otherwise
- return binary_search(array, l, m - 1)
- return -1
Example
Let us see the following implementation to get better understanding −
def binary_search(array, l, r): if (l <= r): m = (l + r) // 2; if (array[m - 1] < array[m] and array[m] > array[m + 1]): return m; if (array[m] < array[m + 1]): return binary_search(array, m + 1,r); else: return binary_search(array, l, m - 1); return -1; array = [7, 8, 9, 12, 10, 6, 3, 2] n = len(array); index = binary_search(array, 1, n-2); if (index != -1): print(array[index]);
Input
[7, 8, 9, 12, 10, 6, 3, 2]
Output
12