Given a bitonic array find the maximum value of the array. An array is said to be bitonic if it has an increasing sequence of integers followed immediately by a decreasing sequence of integers
belongs to collection: Interview C++ coding problems/challenges |searching
All Answers
total answers (1)

C++ programming
Definitely the brute force solution even works in linear time where you just pick each element and compare with the previous & next element. That’s pretty simple. But the concept of bitonic search leads up to more optimized solution reducing the number of comparison.
Algorithm:
Pre-requisite:
Bitonic array, low index (lower bound), high index (upper bound)
FUNCTION findMaxBitonic (Input array, low, high){ While(low<=high){ 1. Set mid to (low+high)/2; 2. Comparisons IF(array [mid]>array[mid-1] &&array[mid]>array[mid+1]) Return array[mid]; //as this is the maximum //in the increasing region of the bitonic array IF(array[mid]>array[mid-1] &&array[mid]<array[mid+1]) low=mid+1; //move lower bound up //in the decreasing region of the bitonic array ELSE IF(array[mid]<array[mid-1] &&array[mid+1]<array[mid]) high=mid-1; //move upper bound down END WHILE IF control comes out of the loop //for trivial cases like array size 1 or 2 return array[array size-1]; END FUNCTIONTime complexity: O(log(n))
C++ implementation
Output
Explanation with example:
So, in this input case we only need one comparison, where if we had done in brute force, would have required 3 comparisons.
need an explanation for this answer? contact us directly to get an explanation for this answer