Split array (0..n-1 permutation) into max chunks so each sorted separately gives sorted array.

Input: arr=[4,3,2,1,0] → Output: 1 Input: arr=[1,0,2,3,4] → Output: 4

Greedy: at each index, if max so far equals index, a valid chunk ends here.

public int maxChunksToSorted(int[] arr) { int max = 0, chunks = 0; for (int i = 0; i < arr.length; i++) { max = Math.max(max, arr[i]); if (max == i) chunks++; } return chunks; }