Max Chunks To Make Sorted - Problem

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

example_1.py โ€” Basic Case
$ Input: [1, 0, 3, 2, 4]
โ€บ Output: 3
๐Ÿ’ก Note: We can split as [1,0] | [3,2] | [4]. After sorting each chunk: [0,1] | [2,3] | [4], which gives us [0,1,2,3,4]. This is 3 chunks, which is maximum possible.
example_2.py โ€” Already Sorted
$ Input: [0, 1, 2, 3, 4]
โ€บ Output: 5
๐Ÿ’ก Note: Array is already sorted, so we can make each element its own chunk: [0] | [1] | [2] | [3] | [4]. This gives us the maximum of 5 chunks.
example_3.py โ€” Reverse Sorted
$ Input: [4, 3, 2, 1, 0]
โ€บ Output: 1
๐Ÿ’ก Note: The array is completely reverse sorted. We can only make 1 chunk [4,3,2,1,0] because any smaller chunk would not contain the proper elements to sort correctly.

Visualization

Tap to expand
Library Book Sorting VisualizationBooks on shelf: [1, 0, 3, 2, 4]10324Step 1: Position 0, Book=1, Max=1, 1โ‰ 0 โ†’ ContinueStep 2: Position 1, Book=0, Max=1, 1=1 โœ“ โ†’ End Section 1Step 3: Position 2, Book=3, Max=3, 3โ‰ 2 โ†’ ContinueStep 4: Position 3, Book=2, Max=3, 3=3 โœ“ โ†’ End Section 2Step 5: Position 4, Book=4, Max=4, 4=4 โœ“ โ†’ End Section 3Final sections marked:10324Section 1: [1,0]Section 2: [3,2]Section 3: [4]After sorting each section:01234Perfect order achieved! 3 sections maximum.
Understanding the Visualization
1
Walk the Shelf
Start from the left, keeping track of the highest book number you've seen
2
Find Boundaries
When the highest number equals your position, you can end a section
3
Why It Works
This ensures all books 0 through position i are in the first i+1 spots
4
Count Sections
Each valid boundary gives you one more section to work with
Key Takeaway
๐ŸŽฏ Key Insight: We can end a chunk at position i when max_seen_so_far equals i, because this guarantees all elements 0 through i are present in positions 0 through i.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array, constant work per element

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables to track maximum and chunk count

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค arr.length โ‰ค 104
  • 0 โ‰ค arr[i] < arr.length
  • arr is a permutation of [0, 1, ..., arr.length - 1]
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
43.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen