# 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] and [5]. The minimum of the 2 consecutive sub-arrays is 1 and 5.
Maximum(1, 5) = 5. This splitting ensures maximum possible value.

Input: arr[] = {-4, -5, -3, -2, -1}, K = 1
Output: -5
Only one sub-array is possible. Hence, min(-4, -5, -3, -2, -1) = -5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The solution can be split into 3 possible cases:

1. When K = 1: In this case, the answer is always equal to the minimum of the array, since the array is split into only one sub-array i.e the array itself.
2. When K ≥ 3: In this case, the answer is always equal to the maximum of the array. When the array has to be split into 3 or more segments, then always keep one segment containing only a single element from the array i.e. the maximum element.
3. When K = 3: This is the trickiest case. There will only be a prefix and a suffix as there can be only two sub-arrays. Maintain an array of prefix minimums and suffix minimums. Then for every element arr[i], update ans = max(ans, min(prefix min value at i, suffix minimum value at i + 1)).

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to return maximum possible value ` `// of maximum of minimum of K sub-arrays ` `int` `maximizeMinimumOfKSubarrays(``const` `int``* arr, ``int` `n, ``int` `k) ` `{ ` `    ``int` `m = INT_MAX; ` `    ``int` `M = INT_MIN; ` ` `  `    ``// Compute maximum and minimum ` `    ``// of the array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``m = min(m, arr[i]); ` `        ``M = max(M, arr[i]); ` `    ``} ` ` `  `    ``// If k = 1 then return the ` `    ``// minimum of the array ` `    ``if` `(k == 1) { ` `        ``return` `m; ` `    ``} ` `    ``// If k >= 3 then return the ` `    ``// maximum of the array ` `    ``else` `if` `(k >= 3) { ` `        ``return` `M; ` `    ``} ` ` `  `    ``// If k = 2 then maintain prefix ` `    ``// and suffix minimums ` `    ``else` `{ ` ` `  `        ``// Arrays to store prefix ` `        ``// and suffix minimums ` `        ``int` `L[n], R[n]; ` ` `  `        ``L[0] = arr[0]; ` `        ``R[n - 1] = arr[n - 1]; ` ` `  `        ``// Prefix minimum ` `        ``for` `(``int` `i = 1; i < n; i++) ` `            ``L[i] = min(L[i - 1], arr[i]); ` ` `  `        ``// Suffix minimum ` `        ``for` `(``int` `i = n - 2; i >= 0; i--) ` `            ``R[i] = min(R[i + 1], arr[i]); ` ` `  `        ``int` `maxVal = INT_MIN; ` ` `  `        ``// Get the maximum possible value ` `        ``for` `(``int` `i = 0; i < n - 1; i++) ` `            ``maxVal = max(maxVal, max(L[i], R[i + 1])); ` ` `  `        ``return` `maxVal; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 3, 4, 5 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``int` `k = 2; ` ` `  `    ``cout << maximizeMinimumOfKSubarrays(arr, n, k); ` ` `  `    ``return` `0; ` `} `

## PHP

= 3 then return the
// maximum of the array
else if (\$k >= 3)
{
return \$M;
}

// If k = 2 then maintain prefix
// and suffix minimums
else
{

// Arrays to store prefix
// and suffix minimums
\$L[0] = \$arr[0];
\$R[\$n – 1] = \$arr[\$n – 1];

// Prefix minimum
for (\$i = 1; \$i < \$n; \$i++) \$L[\$i] = min(\$L[\$i - 1], \$arr[\$i]); // Suffix minimum for (\$i = \$n - 2; \$i >= 0; \$i–)
\$R[\$i] = min(\$R[\$i + 1], \$arr[\$i]);

\$maxVal = PHP_INT_MIN;

// Get the maximum possible value
for (\$i = 0; \$i < \$n - 1; \$i++) \$maxVal = max(\$maxVal, max(\$L[\$i], \$R[\$i + 1])); return \$maxVal; } } // Driver code \$arr = array( 1, 2, 3, 4, 5 ); \$n = sizeof(\$arr); \$k = 2; echo maximizeMinimumOfKSubarrays(\$arr, \$n, \$k); // This code is contributed // by Akanksha Rai ?>

Output:

```5
```

My Personal Notes arrow_drop_up