#K71767. Minimum Length Subarray Reversal

    ID: 33605 Type: Default 1000ms 256MiB

Minimum Length Subarray Reversal

Minimum Length Subarray Reversal

Given a list of integers, Bob wants to sort the list into non-decreasing order by reversing exactly one continuous subarray. Your task is to determine the minimum length of the subarray that must be reversed so that the entire array becomes sorted.

If the array is already sorted, the answer is 0. Otherwise, consider indices l and r (with 0 \leq l \leq r < n) such that reversing the subarray from index l to r makes the whole array sorted. Mathematically, the length of this segment is given by:

[ L = r - l + 1 ]

You need to compute and output the minimal such L.

inputFormat

The input is given via stdin and consists of two lines:

  • The first line contains an integer n (the number of elements in the array).
  • The second line contains n space-separated integers representing the array.

outputFormat

Output a single integer on stdout representing the minimum length of the subarray which, when reversed, will make the whole array sorted in non-decreasing order.

## sample
5
3 1 2 4 5
3