Maximum Partitions to Make a Permutation Sorted

Last Updated : 31 Jul, 2026

Given an array arr[] of size n containing a permutation of integers from 0 to n - 1. Divide the array into contiguous partitions such that after sorting each partition individually and concatenating them in the original order, the entire array becomes sorted. Return the maximum number of partitions that can be formed.

Examples: 

Input: arr[] = [2, 1, 0, 3]
Output: 2
Explanation: The array can be partitioned into [2, 1, 0] and [3]. After sorting each partition, we get [0, 1, 2] and [3]. Concatenating them results in [0, 1, 2, 3], which is sorted.

Input: arr[] = [2, 1, 0, 3, 4, 5]
Output: 4
Explanation: The array can be partitioned into [2, 1, 0], [3], [4], and [5]. Sorting each partition individually and concatenating them produces the sorted array [0, 1, 2, 3, 4, 5].

Input: arr[] = [2, 1, 0]
Output: 1
Explanation: The entire array must be treated as a single partition. After sorting it, we obtain [0, 1, 2].

Try It Yourself
redirect icon

[Naive Approach] Check Maximum for Every Prefix - O(n ^ 2) Time and O(1) Space

The idea is to check every prefix independently. For each index, find the maximum element in the prefix arr[0...i]. If the maximum equals the current index, then all elements from 0 to i are present in that prefix, so a partition can end there.

Working of Approach:

  • Traverse every index of the array.
  • For each index, scan the prefix and find its maximum element.
  • If the maximum element equals the current index, increment the partition count.
  • Return the total number of partitions.
C++
#include <iostream>
#include <vector>
using namespace std;

int maxPartitions(vector<int> &arr)
{
    int n = arr.size();
    int res = 0;

    // Traverse every prefix of the array.
    for (int i = 0; i < n; i++)
    {
        int mx = arr[0];

        // Find the maximum element in the current prefix.
        for (int j = 1; j <= i; j++)
            mx = max(mx, arr[j]);

        // If the maximum element is equal to the current index,
        // then a partition can end at this index.
        if (mx == i)
            res++;
    }

    return res;
}

int main()
{
    vector<int> arr = {2, 1, 0, 3, 4, 5};

    cout << maxPartitions(arr);

    return 0;
}
Java
import java.util.Arrays;

public class GFG {

    public static int maxPartitions(int[] arr)
    {
        int n = arr.length;
        int res = 0;

        // Traverse every prefix of the array.
        for (int i = 0; i < n; i++) {
            int mx = arr[0];

            // Find the maximum element in the current
            // prefix.
            for (int j = 1; j <= i; j++)
                mx = Math.max(mx, arr[j]);

            // If the maximum element is equal to the
            // current index, then a partition can end at
            // this index.
            if (mx == i)
                res++;
        }

        return res;
    }

    public static void main(String[] args)
    {
        int[] arr = { 2, 1, 0, 3, 4, 5 };

        System.out.println(maxPartitions(arr));
    }
}
Python
def maxPartitions(arr):
    n = len(arr)
    res = 0

    # Traverse every prefix of the array.
    for i in range(n):
        mx = arr[0]

        # Find the maximum element in the current prefix.
        for j in range(1, i + 1):
            mx = max(mx, arr[j])

        # If the maximum element is equal to the current index,
        # then a partition can end at this index.
        if mx == i:
            res += 1

    return res


if __name__ == '__main__':
    arr = [2, 1, 0, 3, 4, 5]
    print(maxPartitions(arr))
C#
using System;

public class GFG {
    public static int maxPartitions(int[] arr)
    {
        int n = arr.Length;
        int res = 0;

        // Traverse every prefix of the array.
        for (int i = 0; i < n; i++) {
            int mx = arr[0];

            // Find the maximum element in the current
            // prefix.
            for (int j = 1; j <= i; j++)
                mx = Math.Max(mx, arr[j]);

            // If the maximum element is equal to the
            // current index, then a partition can end at
            // this index.
            if (mx == i)
                res++;
        }

        return res;
    }

    public static void Main()
    {
        int[] arr = { 2, 1, 0, 3, 4, 5 };

        Console.WriteLine(maxPartitions(arr));
    }
}
JavaScript
function maxPartitions(arr)
{
    let n = arr.length;
    let res = 0;

    // Traverse every prefix of the array.
    for (let i = 0; i < n; i++) {
        let mx = arr[0];

        // Find the maximum element in the current prefix.
        for (let j = 1; j <= i; j++) {
            mx = Math.max(mx, arr[j]);
        }

        // If the maximum element is equal to the current
        // index, then a partition can end at this index.
        if (mx === i) {
            res++;
        }
    }

    return res;
}

// Driver Code
let arr = [ 2, 1, 0, 3, 4, 5 ];
console.log(maxPartitions(arr));

Output
4

[Expected Approach] Using Prefix Maximum (Greedy) - O(n) Time and O(1) Space

The idea is to maintain the maximum element seen so far while traversing the array. Whenever the maximum element seen so far becomes equal to the current index, all values from 0 to i must lie within the current prefix because the array is a permutation of 0 to n - 1. Hence, this prefix forms a valid partition, so we greedily end the current partition here.

Working of Approach:

  • Initialize maxSoFar = 0 and res = 0.
  • Traverse the array from left to right.
  • Update maxSoFar with the maximum element seen so far.
  • If maxSoFar == current index, increment res as a partition can end here.
  • Return res.

Let us understand with an example:
Input: arr[] = [2, 1, 0, 3, 4, 5]

  • At index 0, the maximum element seen so far is 2, so no partition can be made.
  • At index 2, the maximum element becomes equal to the index (2), so the first partition is [2, 1, 0].
  • At indices 3, 4, and 5, the maximum element again equals the current index, so each element forms its own partition.
  • Thus, the partitions are [2, 1, 0], [3], [4], and [5].
  • Hence, the maximum number of partitions is 4.
C++
#include <iostream>
#include <vector>
using namespace std;

int maxPartitions(vector<int> &arr)
{
    int n = arr.size();
    int res = 0, maxSoFar = 0;
    for (int i = 0; i < n; ++i)
    {

        // Find maximum in prefix arr[0..i]
        maxSoFar = max(maxSoFar, arr[i]);

        // If maximum so far is equal to index,
        // we can make a new partition ending at
        // index i.
        if (maxSoFar == i)
            res++;
    }
    return res;
}

int main()
{
    vector<int> arr = {2, 1, 0, 3, 4, 5};

    cout << maxPartitions(arr);

    return 0;
}
Java
import java.util.*;

class GFG {

    static int maxPartitions(int[] arr)
    {
        int n = arr.length;
        int res = 0, maxSoFar = 0;

        for (int i = 0; i < n; i++) {

            // Find maximum in prefix arr[0..i]
            maxSoFar = Math.max(maxSoFar, arr[i]);

            // If maximum so far is equal to index,
            // we can make a new partition ending at index
            // i.
            if (maxSoFar == i)
                res++;
        }

        return res;
    }

    public static void main(String[] args)
    {
        int[] arr = { 2, 1, 0, 3, 4, 5 };

        System.out.println(maxPartitions(arr));
    }
}
Python
def maxPartitions(arr):
    n = len(arr)
    res = 0
    maxSoFar = 0
    for i in range(n):
        # Find maximum in prefix arr[0..i]
        maxSoFar = max(maxSoFar, arr[i])
        # If maximum so far is equal to index,
        # we can make a new partition ending at
        # index i.
        if maxSoFar == i:
            res += 1
    return res


if __name__ == '__main__':
    arr = [2, 1, 0, 3, 4, 5]
    print(maxPartitions(arr))
C#
using System;

class GFG {
    static int maxPartitions(int[] arr)
    {
        int n = arr.Length;
        int res = 0, maxSoFar = 0;

        for (int i = 0; i < n; i++) {
            // Find maximum in prefix arr[0..i]
            maxSoFar = Math.Max(maxSoFar, arr[i]);

            // If maximum so far is equal to index,
            // we can make a new partition ending at index
            // i.
            if (maxSoFar == i)
                res++;
        }

        return res;
    }

    static void Main()
    {
        int[] arr = { 2, 1, 0, 3, 4, 5 };

        Console.WriteLine(maxPartitions(arr));
    }
}
JavaScript
function maxPartitions(arr)
{
    let n = arr.length;
    let res = 0, maxSoFar = 0;
    for (let i = 0; i < n; ++i) {
        // Find maximum in prefix arr[0..i]
        maxSoFar = Math.max(maxSoFar, arr[i]);
        // If maximum so far is equal to index,
        // we can make a new partition ending at
        // index i.
        if (maxSoFar === i)
            res++;
    }
    return res;
}

// Driver Code
let arr = [ 2, 1, 0, 3, 4, 5 ];
console.log(maxPartitions(arr));

Output
4
Comment