#K91562. Minimum Reverse Operations

    ID: 38003 Type: Default 1000ms 256MiB

Minimum Reverse Operations

Minimum Reverse Operations

You are given an array of integers. In one operation, you can reverse a contiguous subarray. Your task is to determine the minimum number of reverse subarray operations required to sort the array in increasing order.

The answer will always be one of the three values: 0, 1, or 2:

  • If the array is already sorted, no operation is needed, so the answer is \(0\).
  • If reversing one contiguous subarray (i.e., identifying a segment that is in strict reverse order relative to the sorted array) sorts the array, then the answer is \(1\).
  • Otherwise, the array can be sorted with two operations, so the answer is \(2\).

Note: Make sure to read input from standard input (stdin) and output your answers to standard output (stdout).

inputFormat

The first line contains a single integer \(T\) representing the number of test cases. Each test case is described as follows:

  • The first line of each test case contains an integer \(N\), the size of the array.
  • The second line contains \(N\) space-separated integers representing the array.

outputFormat

For each test case, output a single line with one integer: the minimum number of reverse operations required to sort the given array.

## sample
2
5
3 1 2 5 4
4
4 3 2 1
2

1

</p>