# Solution For Max Chunks To Make Sorted Ii

Problem Statement:
Given an array of integers (not necessarily distinct), we split the array into some number of “chunks” (partitions), and individually sort each chunk. After concatenating them, the result equals the sorted array.

What is the most number of chunks we could have made?

Example 1:
Input: [5,4,3,2,1] Output: 1
Explanation:
Splitting into two or more chunks will not return the required sorted array. So, the most number of chunks we can make is 1.

Example 2:
Input: [2,1,3,4,4] Output: 4
Explanation:
We can split the given array into 4 chunks, each of which is, [2,1], , , and . After sorting each chunk separately, we can concatenate them to get the sorted version of the original array.

Approach:
To solve this problem, we will follow a two-step approach.

Step 1: Create two arrays
We will create two arrays, leftMax and rightMin, of size n (length of array). Each element of both arrays will represent the maximum element from the start of the array to that index, and the minimum element from that index to the end of the array, respectively.

Step 2: Count number of chunks
We can split the array into chunks such that each chunk is sorted, if the maximum element of the first chunk is smaller than or equal to the minimum element of the second chunk, and so on. We can keep track of the number of such valid chunks, and return that as the answer.

Solution:

class Solution {
public:
int maxChunksToSorted(vector& arr) {
int n = arr.size();
vector leftMax(n), rightMin(n);

``````    leftMax = arr;
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 = 1;
for(int i = 0; i < n-1; i++) {
if(leftMax[i] <= rightMin[i+1]) {
count++;
}
}

return count;
}
``````

};

Time Complexity:
Creating both leftMax and rightMin arrays will take O(n) time. The loop to count valid chunks will also take O(n) time. Therefore, the overall time complexity of the solution is O(n).

Space Complexity:
We are using two extra integer arrays of size n, one to store the maximum values from the start, and the other to store the minimum values from the end. Therefore, the space complexity of the solution is O(n).

## Step by Step Implementation For Max Chunks To Make Sorted Ii

```class Solution {
public int maxChunksToSorted(int[] arr) {
//maxChunksToSorted will return the number of chunks
//needed to be sorted in order to have a sorted array

//we will use a greedy approach to solve this problem
//the idea is that we want to make sure each chunk is as
//sorted as possible before combining it with other chunks

//we will use two arrays, max and min, to keep track of the
//maximum and minimum values seen so far
//as we iterate through the array, we update these values
//accordingly
//if we ever see a value that is greater than the maximum value
//seen so far, we know that we need to create a new chunk
//similarly, if we see a value that is less than the minimum value
//seen so far, we know that we need to create a new chunk

//we can keep track of the number of chunks needed by incrementing
//a counter whenever we create a new chunk

//finally, we return the number of chunks needed

if (arr == null || arr.length == 0) {
return 0;
}

int n = arr.length;
int[] max = new int[n];
int[] min = new int[n];

max = arr;
min = arr;

for (int i = 1; i < n; i++) {
max[i] = Math.max(max[i-1], arr[i]);
min[i] = Math.min(min[i-1], arr[i]);
}

int result = 1;
for (int i = 1; i < n; i++) {
if (max[i] > min[i-1]) {
result++;
}
}

return result;
}
}```
```def max_chunks_to_make_sorted_ii(arr):
# keep track of the maximum and minimum values seen so far
max_seen = arr
min_seen = arr

# keep track of the number of chunks seen so far
chunks = 0

# iterate through the array
for i in range(1, len(arr)):
# update the maximum and minimum values seen so far
max_seen = max(max_seen, arr[i])
min_seen = min(min_seen, arr[i])

# if the current value is greater than or equal to the maximum value seen so far
# and the current value is less than or equal to the minimum value seen so far,
# then we have found another chunk
if arr[i] >= max_seen and arr[i] <= min_seen:
chunks += 1

return chunks```
```//Solution:

/**
* @param {number[]} arr
* @return {number}
*/
var maxChunksToSorted = function(arr) {
// create a copy of the array
var copy = arr.slice();

// sort the copy
copy.sort(function(a, b) {
return a-b;
});

// create a variable to keep track of the number of chunks
var chunkCount = 0;

// iterate through the array
for (var i = 0; i < arr.length; i++) {
// if the element at index i in the array is equal to the element at index i in the copy
if (arr[i] === copy[i]) {
// increment chunkCount
chunkCount++;
}
}

return chunkCount;
};```
```There are two ways to approach this problem. One is to use a greedy algorithm, and the other is to use a dynamic programming algorithm.

Greedy algorithm:

1. We keep track of the maximum value seen so far and the number of chunks needed to sort the array.
2. We iterate through the array and update the maximum value seen so far.
3. If the current element is less than or equal to the maximum value seen so far, we increment the number of chunks needed.
4. Otherwise, we reset the maximum value seen so far to the current element.

Dynamic programming algorithm:

1. We create an array dp of size n, where dp[i] represents the minimum number of chunks needed to sort the subarray arr[0..i].
2. We initialize dp = 0.
3. We iterate through the array and for each element arr[i], we find the maximum value seen so far among all the previous elements arr[0..i-1].
4. If the maximum value seen so far is less than or equal to arr[i], we can update dp[i] = dp[i-1] + 1. Otherwise, we set dp[i] = dp[i-1].

Both algorithms have the same time complexity of O(n), where n is the size of the array. The space complexity of the greedy algorithm is O(1), while the space complexity of the dynamic programming algorithm is O(n).```
`public int MaxChunksToSorted(int[] arr) { // This problem is similar to "Max Chunks to Make Sorted" // However, in this problem, the array may not be // strictly increasing or decreasing, so we need to // keep track of the maximum and minimum values seen // so far. int n = arr.Length; int[] maxOfLeft = new int[n]; int[] minOfRight = new int[n]; // Initialize the maxOfLeft array maxOfLeft = arr; for (int i = 1; i < n; i++) { maxOfLeft[i] = Math.Max(maxOfLeft[i-1], arr[i]); } // Initialize the minOfRight array minOfRight[n-1] = arr[n-1]; for (int i = n - 2; i >= 0; i--) { minOfRight[i] = Math.Min(minOfRight[i+1], arr[i]); } // We can get the maximum number of chunks by comparing // the maxOfLeft array with the minOfRight array. int result = 0; for (int i = 0; i < n - 1; i++) { if (maxOfLeft[i] <= minOfRight[i+1]) { result++; } } // The last element will always be in a chunk by itself, // so we need to add 1 to the result. return result + 1; }`

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]