703450. Maximum number of partitions that can be sorted individually to make sorted

Easy

Maximum Number of Partitions that can be Sorted Individually to make Sorted

Summary

Given an unsorted array, find the maximum number of partitions that can be sorted individually to make the entire array sorted. This problem involves sorting arrays and partitioning them into smaller subarrays. The key concept is to identify the optimal way to split the array into smaller parts, ensuring that each part can be sorted independently.

Detailed Explanation

To solve this problem, we will use a divide-and-conquer approach. We will first find the middle element of the array and then partition it into two halves. Then, we will recursively apply this process to each half until the base case is reached (i.e., when the length of the subarray becomes 1).

Here's a step-by-step breakdown of the solution:

  • Find the middle element of the array.
  • Partition the array into two halves: left and right.
  • Recursively apply the same process to each half until the base case is reached.
  • Combine the results from the left and right partitions to form the final sorted array.

Time complexity analysis: The time complexity of this algorithm is O(n log n), where n is the length of the input array. This is because we are using a divide-and-conquer approach, which involves recursively partitioning the array into smaller subarrays. The space complexity is O(log n) due to the recursive function calls.

Optimized Solutions

Java

View on GeeksforGeeks
public class MaximumSortedPartitions {
    public static int maxPartitions(int[] arr) {
        if (arr == null || arr.length == 0) return 0;
        
        int n = arr.length;
        int left = 0, right = n - 1;
        int partitions = 0;
        
        while (left < right) {
            if (arr[left] <= arr[right]) {
                left++;
            } else {
                right--;
            }
            partitions++;
        }
        
        return partitions + 1;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.