key: mx == i.

1
2
3
4
5
6
7
8
public int maxChunksToSorted(int[] arr) {
int res = 0, mx = 0;
for (int i = 0; i < arr.length; ++i) {
mx = Math.max(mx, arr[i]);
if (mx == i) ++res;
}
return res;
}