Max Chunks To Make Sorted

Medium

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:

Can't split.

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:

Cannot split this array. If trying [2] and [0,1], sorting gives [2] + [0,1] = [2,0,1] which is not sorted. need all elements together: [2,0,1] sorts to [0,1,2].

Constraints

  • 1 ≤ arr.length ≤ 10

Ready to solve this problem?

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