KatInterview

780. Max Chunks To Make Sorted

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the maximum number of partitions (chunks) we can split an array arr into, such that if we sort each chunk individually and then concatenate them, the result is the sorted version of the original array. The input arr is a permutation of integers from 0 to n-1, where n is the length of the array. The sorted version of arr will always be [0, 1, ..., n-1].

Solution

Max Chunks To Make Sorted in Python

class Solution:
    def maxChunksToSorted(self, arr: List[int]) -> int:
        chunks = 0
        max_so_far = 0
        
        for i, num in enumerate(arr):
            # Update the maximum value seen so far in the prefix arr[0...i]
            max_so_far = max(max_so_far, num)
            
            # If the maximum element in the prefix arr[0...i] is equal to i,
            # it means all elements from 0 to i are present in this prefix because
            # the array is a permutation of [0, n-1].
            # This forms a valid chunk.
            if max_so_far == i:
                chunks += 1
                
        return chunks

Max Chunks To Make Sorted in Java

class Solution {
    public int maxChunksToSorted(int[] arr) {
        int n = arr.length;

        int chunks = 0;
        int maxSoFar = 0;
        
        for (int i = 0; i < n; i++) {
            // Update the maximum value seen so far in the prefix arr[0...i]
            maxSoFar = Math.max(maxSoFar, arr[i]);
            
            // If the maximum element in the prefix arr[0...i] is equal to i,
            // it means all elements from 0 to i are present in this prefix because
            // the array is a permutation of [0, n-1].
            // This forms a valid chunk.
            if (maxSoFar == i) {
                chunks++;
            }
        }
        
        return chunks;
    }
}

Max Chunks To Make Sorted in C++

class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int n = arr.size();
        
        int chunks = 0;
        int max_so_far = 0;
        
        for (int i = 0; i < n; ++i) {
            // Update the maximum value seen so far in the prefix arr[0...i]
            max_so_far = max(max_so_far, arr[i]);
            
            // If the maximum element in the prefix arr[0...i] is equal to i,
            // it means all elements from 0 to i are present in this prefix because
            // the array is a permutation of [0, n-1].
            // This forms a valid chunk.
            if (max_so_far == i) {
                chunks++;
            }
        }
        
        return chunks;
    }
};

Max Chunks To Make Sorted in TypeScript

function maxChunksToSorted(arr: number[]): number {
    const n = arr.length;

    let chunks = 0;
    let maxSoFar = 0;

    for (let i = 0; i < n; i++) {
        // Update the maximum value seen so far in the prefix arr[0...i]
        maxSoFar = Math.max(maxSoFar, arr[i]);

        // If the maximum element in the prefix arr[0...i] is equal to i,
        // it means all elements from 0 to i are present in this prefix because
        // the array is a permutation of [0, n-1].
        // This forms a valid chunk.
        if (maxSoFar === i) {
            chunks++;
        }
    }

    return chunks;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.