Maximize partitions that if sorted individually makes the whole Array sorted
Last Updated :
27 Mar, 2025
Given an array arr[]. The task is to divide arr[] into the maximum number of partitions, such that, those partitions if sorted individually make the whole array sorted.
Examples:
Input: arr[] = { 28, 9, 18, 32, 60, 50, 75, 70 }
Output: 4
Explanation: Following are the partitions in which the array is divided.
If we divide arr[] into four partitions {28, 9, 18}, {32}, { 60, 50}, and {75, 70}, sort them and concatenate.
Sorting all of them indivudually makes the whole array sorted.
Hence, 4 is the answer.
Input: arr[] = { 2, 1, 0, 3, 4, 5 }
Output: 4
Approach: This problem is implementation-based. Follow the steps below to solve the given problem.
- Create a maximum array that calculates the maximum element to the left till that index of the array.
- Create a minimum array that calculates the minimum element to the right till that index of the array.
- Iterate through the array, each time all elements to the leftMax[] are smaller (or equal) to all elements to the rightMin[], that means there is a new chunk, so increment the count by 1.
- Return count+1 as the final answer.
Below is the implementation of the above approach.
C++
// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum partitions.
int maxPartitions(vector<int>& arr)
{
int N = arr.size();
// To keep track of max
// and min elements at every index
vector<int> leftMax(arr.size());
vector<int> rightMin(arr.size());
leftMax[0] = arr[0];
for (int i = 1; i < N; i++) {
leftMax[i] = max(leftMax[i - 1],
arr[i]);
}
rightMin[N - 1] = arr[N - 1];
for (int i = N - 2; i >= 0; i--) {
rightMin[i] = min(rightMin[i + 1],
arr[i]);
}
int count = 0;
for (int i = 0; i < N - 1; i++) {
if (leftMax[i] <= rightMin[i + 1]) {
count++;
}
}
// Return count + 1 as the final answer
return count + 1;
}
// Driver code
int main()
{
vector<int> arr{ 10, 0, 21, 32, 68 };
cout << maxPartitions(arr);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
public class GFG {
// Function to find maximum partitions.
static int maxPartitions(int[] arr)
{
int N = arr.length;
// To keep track of max
// and min elements at every index
int[] leftMax = new int[arr.length];
int[] rightMin = new int[arr.length];
leftMax[0] = arr[0];
for (int i = 1; i < N; i++) {
leftMax[i] = Math.max(leftMax[i - 1], arr[i]);
}
rightMin[N - 1] = arr[N - 1];
for (int i = N - 2; i >= 0; i--) {
rightMin[i] = Math.min(rightMin[i + 1], arr[i]);
}
int count = 0;
for (int i = 0; i < N - 1; i++) {
if (leftMax[i] <= rightMin[i + 1]) {
count++;
}
}
// Return count + 1 as the final answer
return count + 1;
}
// Driver code
public static void main(String args[])
{
int[] arr = { 10, 0, 21, 32, 68 };
System.out.println(maxPartitions(arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Python
# Python program for above approach
# Function to find maximum partitions.
def maxPartitions(arr):
N = len(arr)
# To keep track of max
# and min elements at every index
leftMax = []
rightMin = []
leftMax.append(arr[0])
for i in range(1, N):
leftMax.append(max(leftMax[i - 1], arr[i]))
rightMin.append(arr[N - 1])
for i in range(1, N):
rightMin.append(min(rightMin[i - 1], arr[N - i - 1]))
rightMin.reverse()
count = 0
for i in range(0, N - 1):
if (leftMax[i] <= rightMin[i + 1]):
count = count + 1
# Return count + 1 as the final answer
return count + 1
# Driver code
arr = [10, 0, 21, 32, 68]
print(maxPartitions(arr))
# This code is contributed by Samim Hossain Mondal.
C#
// C# program for above approach
using System;
class GFG {
// Function to find maximum partitions.
static int maxPartitions(int[] arr)
{
int N = arr.Length;
// To keep track of max
// and min elements at every index
int[] leftMax = new int[arr.Length];
int[] rightMin = new int[arr.Length];
leftMax[0] = arr[0];
for (int i = 1; i < N; i++) {
leftMax[i] = Math.Max(leftMax[i - 1], arr[i]);
}
rightMin[N - 1] = arr[N - 1];
for (int i = N - 2; i >= 0; i--) {
rightMin[i] = Math.Min(rightMin[i + 1], arr[i]);
}
int count = 0;
for (int i = 0; i < N - 1; i++) {
if (leftMax[i] <= rightMin[i + 1]) {
count++;
}
}
// Return count + 1 as the final answer
return count + 1;
}
// Driver code
public static void Main()
{
int[] arr = { 10, 0, 21, 32, 68 };
Console.WriteLine(maxPartitions(arr));
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript code for the above approach
// Function to find maximum partitions.
function maxPartitions(arr)
{
let N = arr.length;
// To keep track of max
// and min elements at every index
let leftMax = new Array(arr.length);
let rightMin = new Array(arr.length);
leftMax[0] = arr[0];
for (let i = 1; i < N; i++) {
leftMax[i] = Math.max(leftMax[i - 1],
arr[i]);
}
rightMin[N - 1] = arr[N - 1];
for (let i = N - 2; i >= 0; i--) {
rightMin[i] = Math.min(rightMin[i + 1],
arr[i]);
}
let count = 0;
for (let i = 0; i < N - 1; i++) {
if (leftMax[i] <= rightMin[i + 1]) {
count++;
}
}
// Return count + 1 as the final answer
return count + 1;
}
// Driver code
let arr = [10, 0, 21, 32, 68];
document.write(maxPartitions(arr));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Maximum number of partitions that can be sorted individually to make sorted Given an array arr[] of size n such that elements of arr[] in range [0, 1, ..n-1] where every number is present at most once. Our task is to divide the array into maximum number of partitions that can be sorted individually, then concatenated to make the whole array sorted.Examples : Input : arr[] =
7 min read
Smallest Subarray to be Sorted to make the whole array sorted Given an unsorted array arr[]. Find the subarray arr[s...e] such that sorting this subarray makes the whole array sorted.Note: If the given array is already sorted then return [0, 0].Examples:Input: arr[] = [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60]Output: [3, 8]Explanation: Sorting subarray start
15+ min read
Maximize score of same-indexed subarrays selected from two given arrays Given two arrays A[] and B[], both consisting of N positive integers, the task is to find the maximum score among all possible same-indexed subarrays in both the arrays such that the score of any subarray over the range [L, R] is calculated by the maximum of the values (AL*BL + AL + 1*BL + 1 + ... +
15+ min read
Longest increasing subsequence which forms a subarray in the sorted representation of the array Given an array arr[] of N integers, the task is to find the length of the longest increasing subsequence such that it forms a subarray when the original array is sorted. Examples: Input: arr[] = { 2, 6, 4, 8, 2, 9 }Output: 3Explanation: Sorted array: {2, 2, 4, 6, 8, 9}All possible non-decreasing seq
11 min read
Split array into two subarrays such that difference of their maximum is minimum Given an array arr[] consisting of integers, the task is to split the given array into two sub-arrays such that the difference between their maximum elements is minimum. Example: Input: arr[] = {7, 9, 5, 10} Output: 1 Explanation: The subarrays are {5, 10} and {7, 9} with the difference between thei
7 min read