Divide an array into k segments to maximize maximum of segment minimums | CPP Program to find maximum value of maximum of minimums of k segments . ; function to calculate the max of all the minimum segments ; if we have to divide it into 1 segment then the min will be the answer ; If k >= 3 , return maximum of all elements . ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOfSegmentMins ( int a [ ] , int n , int k ) { if ( k == 1 ) return * min_element ( a , a + n ) ; if ( k == 2 ) return max ( a [ 0 ] , a [ n - 1 ] ) ; return * max_element ( a , a + n ) ; } int main ( ) { int a [ ] = { -10 , -9 , -8 , 2 , 7 , -6 , -5 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 2 ; cout << maxOfSegmentMins ( a , n , k ) ; }