VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximize-partitions-that-if-sorted-individually-makes-the-whole-array-sorted/

⇱ Maximize partitions that if sorted individually makes the whole Array sorted - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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. 


Output
4

Time Complexity: O(N)
Auxiliary Space: O(N)

Comment