Minimum in an array which is first decreasing then increasing | C ++ program to find the smallest number in an array of decrease and increasing numbers ; Function to find the smallest number 's index ; Do a binary search ; Find the mid element ; Check for break point ; Return the index ; Driver Code ; Print the smallest number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimal ( int a [ ] , int n ) { int lo = 0 , hi = n - 1 ; while ( lo < hi ) { int mid = ( lo + hi ) >> 1 ; if ( a [ mid ] < a [ mid + 1 ] ) { hi = mid ; } else { lo = mid + 1 ; } } return lo ; } int main ( ) { int a [ ] = { 8 , 5 , 4 , 3 , 4 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int ind = minimal ( a , n ) ; cout << a [ ind ] ; }