7
Pos -->. 0 1 2 3 4 5 6 79
Input --> 30 , 10 , 20 , 40 , 60 , 50 , 75 , 7010
<------------> <--> <-------> <------->11
Left Max --> 30 , 30 , 30 , 40 , 60 , 60 , 75 , 7513
Right Min --> 10 , 10 , 20 , 40 , 50 , 50 , 70 , 70 , Integer.max15
1. At pos 2 , left_max 30 is smaller than right_min 40 at pos 316
2. That means , all the elements in the right side of 30 are bigger than all the elements of left side of 30 , including 3017
3. Hence we can break it at pos 2 into a chunk and sort this whole sub-array( 0 - 2 )23
public int maxChunksToSorted(int[] arr) {25
// 1. Generate Right min27
int[] min_from_right = new int[arr.length + 1];28
min_from_right[arr.length] = Integer.MAX_VALUE;30
for (int i = arr.length - 1; i >= 0; i--) {31
min_from_right[i] = Math.min(arr[i], min_from_right[i + 1]);34
// 2. Generate Left Max and Count chunks36
int max_cur = Integer.MIN_VALUE;38
for (int i = 0; i < arr.length; i++) {39
max_cur = Math.max(max_cur, arr[i]);40
if (max_cur <= min_from_right[i + 1]) {