Max Chunks To Make Sorted

MediumArrayBinary SearchMathSorting

Description

Given an array arr that is a permutation of [0, ..., n-1], return the max number of chunks to sort individually then concatenate.

Examples

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

Reversed array: max seen at each index exceeds index until the end. Must sort entire array as one chunk.

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

Array is already sorted, so it is possible to split after each element: [0], [1], [2], [3], [4]. Each chunk when sorted remains the same and concatenating gives the sorted array.

Input:arr = [2,0,1]
Output:1
Explanation:

The array can only be split into 1 chunk without breaking sorted order, so the answer is 1.

Constraints

  • 1 ≤ arr.length ≤ 10

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!