Imagine you have an array that's a permutation of integers from 0 to n-1 (each number appears exactly once). Your goal is to divide this array into chunks such that when you sort each chunk individually and then concatenate them together, you get the sorted array [0, 1, 2, ..., n-1].
The challenge is to find the maximum number of chunks you can create. More chunks means more flexibility in sorting!
Example: For array [1, 0, 3, 2, 4], you can split it as [1, 0] | [3, 2] | [4]. After sorting each chunk: [0, 1] | [2, 3] | [4], which concatenates to [0, 1, 2, 3, 4] โ
This gives us 3 chunks - and that's the maximum possible!
Input & Output
Visualization
Time & Space Complexity
Single pass through the array, constant work per element
Only using a few variables to track maximum and chunk count
Constraints
- 1 โค arr.length โค 104
- 0 โค arr[i] < arr.length
- arr is a permutation of [0, 1, ..., arr.length - 1]