Divide an array into k segments to maximize maximum of segment minimums | 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 Code
function maxOfSegmentMins ( a , n , k ) { if ( k == 1 ) { a . sort ( ) ; return a [ 0 ] ; } if ( k == 2 ) return Math . max ( a [ 0 ] , a [ n - 1 ] ) ; return a [ n - 1 ] ; } var a = [ - 10 , - 9 , - 8 , 2 , 7 , - 6 , - 5 ] ; var n = a . length ; var k = 2 ; document . write ( maxOfSegmentMins ( a , n , k ) ) ;