#C1106. Find the Length of the Shortest Subarray to Sort
Find the Length of the Shortest Subarray to Sort
Find the Length of the Shortest Subarray to Sort
Given an array of integers \(A = [a_1, a_2, \dots, a_n]\), your task is to determine the length of the shortest contiguous subarray such that if you sort this subarray in non-decreasing order, the entire array becomes sorted in non-decreasing order.
In other words, find the minimum length \(L = j - i + 1\) (where \(1 \le i \le j \le n\)) such that when the subarray \(A[i \dots j]\) is sorted, the whole array \(A\) is sorted. If the array is already sorted, output 0.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains an integer \(n\) representing the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
Output via standard output (stdout) a single integer — the length of the shortest subarray that needs to be sorted so that the entire array becomes sorted in non-decreasing order.
## sample5
2 6 4 8 10
2
</p>