#K91542. Minimum Operations to Sort

    ID: 37998 Type: Default 1000ms 256MiB

Minimum Operations to Sort

Minimum Operations to Sort

You are given an array of n balls, each having an integer value. In one operation, you are allowed to reverse a contiguous subarray of the array. The goal is to sort the array in ascending order with the minimum number of operations. It can be proven that the answer is always 0, 1, or 2.

More formally, let the array be \(A = [a_1,a_2,\dots,a_n]\) and let \(A_{sorted}\) be the array \(A\) sorted in non-decreasing order. An operation is defined as choosing two indices \(i\) and \(j\) (with \(1 \le i < j \le n\)) and reversing the segment \(A[i\ldots j]\). Determine the minimum number of such operations required to transform \(A\) into \(A_{sorted}\).

Input constraints: The input will be provided via standard input.

inputFormat

The first line contains a single integer \(n\) (the number of balls). The second line contains \(n\) space-separated integers representing the values of the balls.

outputFormat

Output a single integer: the minimum number of operations required to sort the array in ascending order.

## sample
5
1 2 3 4 5
0