Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Divide the array in K segments such that the sum of minimums is maximized
Next article icon

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

Last Updated : 18 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

  1. k >= 3: When k is greater than 2, one segment will only compose of {max element}, so that max of minimum segments will always be the max.
  2. k = 2: For k = 2 the answer is the maximum of the first and last element.
  3. 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++
// 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 segments 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 *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 function 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); } 
Java
// 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
# Python3 Program to find maximum value of  # maximum of minimums of k segments.  # function to calculate the max of all the  # minimum segments  def 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 if __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#
// 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 // PHP 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)      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. ?> 
JavaScript
<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) 

 


Next Article
Divide the array in K segments such that the sum of minimums is maximized

R

Raja Vikramaditya
Improve
Article Tags :
  • Misc
  • Searching
  • DSA
  • Arrays
Practice Tags :
  • Arrays
  • Misc
  • Searching

Similar Reads

  • Split array into K subsets to maximize their sum of maximums and minimums
    Given an integer K and an array A[ ] whose length is multiple of K, the task is to split the elements of the given array into K subsets, each having an equal number of elements, such that the sum of the maximum and minimum elements of each subset is the maximum summation possible. Examples: Input: K
    6 min read
  • Divide the array in K segments such that the sum of minimums is maximized
    Given an array a of size N and an integer K, the task is to divide the array into K segments such that sum of the minimum of K segments is maximized. Examples: Input: a[] = {5, 7, 4, 2, 8, 1, 6}, K = 3 Output: 7 Divide the array at indexes 0 and 1. Then the segments are {5}, {7}, {4, 2, 8, 1, 6}. Su
    9 min read
  • Maximize the maximum among minimum of K consecutive sub-arrays
    Given an integer K and an array arr[], the task is to split the array arr[] into K consecutive subarrays to find the maximum possible value of the maximum among the minimum value of K consecutive sub-arrays. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 2 Output: 5 Split the array as [1, 2, 3, 4] an
    9 min read
  • Split array into K non-empty subsets such that sum of their maximums and minimums is maximized
    Given two arrays arr[] and S[] consisting of N and K integers, the task is to find the maximum sum of minimum and maximum of each subset after splitting the array into K subsets such that the size of each subset is equal to one of the elements in the array S[]. Examples: Input: arr[] = {1, 13, 7, 17
    7 min read
  • Split given arrays into subarrays to maximize the sum of maximum and minimum in each subarrays
    Given an array arr[] consisting of N integers, the task is to maximize the sum of the difference of the maximum and minimum in each subarrays by splitting the given array into non-overlapping subarrays. Examples: Input: arr[] = {8, 1, 7, 9, 2}Output: 14Explanation:Split the given array arr[] as {8,
    7 min read
  • Maximum of minimums of every window size in a given array
    Given an integer array arr[] of size n, the task is to find the maximum of the minimums for every window size in the given array, where the window size ranges from 1 to n. Example: Input: arr[] = [10, 20, 30]Output: [30, 20, 10]Explanation: First element in output indicates maximum of minimums of al
    14 min read
  • Maximize sum of minimum and maximum of all groups in distribution
    Given an array arr[], and an integer N. The task is to maximize the sum of minimum and maximum of each group in a distribution of the array elements in N groups where the size of each group is given in an array b[] of size N. Examples: Input: a[] = {17, 12, 11, 9, 8, 8, 5, 4, 3}, N = 3, b[] = {2, 3,
    9 min read
  • Distribute given arrays into K sets such that total sum of maximum and minimum elements of all sets is maximum
    Given two arrays, the first arr[] of size N and the second brr[] of size K. The task is to divide the first array arr[] into K sets such that the i-th set should contain brr[i] elements from the second array brr[], and the total sum of maximum and minimum elements of all sets is maximum. Examples: I
    8 min read
  • Maximize the numbers of splits in an Array having sum divisible by 3
    Given an integer array arr of size N. The task is to find the maximum number of splits such that each split has sum divisible by 3. It is not necessary that all splits are divisible by 3, the task is to just maximize the number of splits which are divisible by 3.Examples: Input: arr [] = [2, 36, 1,
    7 min read
  • Split array into subarrays such that sum of difference between their maximums and minimums is maximum
    Given an array arr[] consisting of N integers, the task is to split the array into subarrays such that the sum of the difference between the maximum and minimum elements for all the subarrays is maximum. Examples : Input: arr[] = {8, 1, 7, 9, 2}Output: 14Explanation:Consider splitting the given arra
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences