Divide an array into k segments to maximize maximum of segment minimums - GeeksforGeeks

27 Apr, 2021
Given an array of n integers, divide it into k segments and find the maximum of the minimums of k segments. Output the maximum integer that can be obtained among all ways to segment in k subarrays.Examples:  
Input : arr[] = {1, 2, 3, 6, 5}  
        k = 2
Output: 5
Explanation: There are many ways to create 
two segments. The optimal segments are (1, 2, 3)
and (6, 5). Minimum of both segments are 1 and 5, 
hence the maximum(1, 5) is 5. 


Input: -4 -5 -3 -2 -1 k=1
Output: -5 
Explanation: only one segment, so minimum is -5.
 
There will be 3 cases that need to be considered.  
k >= 3: When k is greater then 2, one segment will only compose of {max element}, so that max of minimum segments will always be the max.k = 2: For k = 2 the answer is the maximum of the first and last element.k = 1: Only possible partition is one segment equal to the whole array. So the answer is the minimum value on the whole array.
k >= 3: When k is greater then 2, one segment will only compose of {max element}, so that max of minimum segments will always be the max.
k = 2: For k = 2 the answer is the maximum of the first and last element.
k = 1: Only possible partition is one segment equal to the whole array. So the answer is the minimum value on the whole array.
Below is the implementation of the above approach  
C++
Java
Python3
C#
PHP
Javascript
// CPP Program to find maximum value of// maximum of minimums of k segments.#include <bits/stdc++.h>using namespace std; // function to calculate the max of all the// minimum segmentsint maxOfSegmentMins(int a[], int n, int k){    // if we have to divide it into 1 segment    // then the min will be the answer    if (k == 1)       return *min_element(a, a+n);     if (k == 2)       return max(a[0], a[n-1]);         // If k >= 3, return maximum of all    // elements.    return *max_element(a, a+n);} // driver program to test the above functionint 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);}
// Java Program to find maximum// value of maximum of minimums// of k segments.import java .io.*;import java .util.*; class GFG{     // function to calculate    // the max of all the    // minimum segments    static int maxOfSegmentMins(int []a,                                int n,                                int k)    {                 // if we have to divide        // it into 1 segment then        // the min will be the answer        if (k == 1)        {            Arrays.sort(a);                return a[0];        }             if (k == 2)            return Math.max(a[0],                            a[n - 1]);                 // If k >= 3, return        // maximum of all        // elements.        return a[n - 1];    }         // Driver Code    static public void main (String[] args)    {        int []a = {-10, -9, -8,                    2, 7, -6, -5};        int n = a.length;        int k = 2;                 System.out.println(                maxOfSegmentMins(a, n, k));    }} // This code is contributed// by anuj_67.
# Python3 Program to find maximum value of# maximum of minimums of k segments. # function to calculate the max of all the# minimum segmentsdef maxOfSegmentMins(a,n,k):     # if we have to divide it into 1 segment    # then the min will be the answer    if k ==1:        return min(a)    if k==2:        return max(a[0],a[n-1])     # If k >= 3, return maximum of all    #  elements.    return max(a)     # Driver codeif __name__=='__main__':    a = [-10, -9, -8, 2, 7, -6, -5]    n = len(a)    k =2    print(maxOfSegmentMins(a,n,k)) # This code is contributed by# Shrikant13
// C# Program to find maximum value of// maximum of minimums of k segments.using System;using System.Linq; public class GFG {      // function to calculate the max    // of all the minimum segments    static int maxOfSegmentMins(int []a,                             int n, int k)    {                 // if we have to divide it into 1        // segment then the min will be        // the answer        if (k == 1)            return a.Min();             if (k == 2)            return Math.Max(a[0], a[n - 1]);                 // If k >= 3, return maximum of        // all elements.        return a.Max();    }         // Driver function    static public void Main ()    {        int []a = { -10, -9, -8, 2, 7,                                 -6, -5 };        int n = a.Length;        int k = 2;                 Console.WriteLine(                  maxOfSegmentMins(a, n, k));    }} // This code is contributed by vt_m.
<?php// PHP Program to find maximum// value of maximum of minimums// of k segments.  // function to calculate// the max of all the// minimum segmentsfunction maxOfSegmentMins($a, $n, $k){    // if we have to divide    // it into 1 segment then    // the min will be the answer    if ($k == 1)    return min($a);     if ($k == 2)    return max($a[0],               $a[$n - 1]);         // If k >= 3, return    // maximum of all elements.    return max($a);} // Driver Code$a = array(-10, -9, -8,            2, 7, -6, -5);$n = count($a);$k = 2;echo maxOfSegmentMins($a, $n, $k); // This code is contributed by vits.?>
<script>// javascript Program to find maximum// value of maximum of minimums// of k segments.   // function to calculate    // the max of all the    // minimum segments    function maxOfSegmentMins(a , n , k)    {         // if we have to divide        // it into 1 segment then        // the min will be the answer        if (k == 1) {            a.sort();            return a[0];        }         if (k == 2)            return Math.max(a[0], a[n - 1]);         // If k >= 3, return        // maximum of all        // elements.        return a[n - 1];    }     // Driver Code        var a = [ -10, -9, -8, 2, 7, -6, -5 ];        var n = a.length;        var k = 2;         document.write(maxOfSegmentMins(a, n, k)); // This code is contributed by Rajput-Ji</script>
Output:  
-5
Time complexity: O(n) Auxiliary Space: O(1) This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
shrikanth13
Rajput-Ji
Arrays
Searching
Arrays
Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Binary Search
Linear Search
Maximum and minimum of an array using minimum number of comparisons
Find the Missing Number
K'th Smallest/Largest Element in Unsorted Array | Set 1