Typical example
Input
arr = [2, 6, 4, 8, 10, 9, 15]
Output
5
Sorting the subarray from index 1 to 5 (6,4,8,10,9) makes the whole array sorted. The window length is 5.
Full lesson preview
Return the length of the smallest subarray that, if sorted, makes the whole array sorted.
Problem statement
Task
Examples
Input
arr = [2, 6, 4, 8, 10, 9, 15]
Output
5
Sorting the subarray from index 1 to 5 (6,4,8,10,9) makes the whole array sorted. The window length is 5.
Input format
Output format
Constraints
Samples
Input
arr = [1, 2, 3, 4, 5]
Output
0
Already sorted array requires no window.