#K85237. Minimum Subarray Reversal Operations

    ID: 36597 Type: Default 1000ms 256MiB

Minimum Subarray Reversal Operations

Minimum Subarray Reversal Operations

You are given two arrays A and B of length n. You are allowed to perform the following operation exactly once:

  • Choose a contiguous subarray of A and reverse it.

Your task is to determine the minimum number of operations required to transform array A into array B. If the transformation is impossible, output -1.

The transformation is only possible if the sorted versions of A and B are identical. If arrays A and B are already equal, then the answer is 0. Otherwise, if there exists a single contiguous subarray which when reversed makes A equal to B, the answer is 1. In all other cases, output -1.

This problem involves checking array equality, verifying frequency counts, and applying a simple reversal in constant time.

inputFormat

The first line contains a single integer T (1 ≤ T ≤ 10), the number of test cases.

For each test case, the first line contains an integer n (1 ≤ n ≤ 105), denoting the length of both arrays A and B.

The second line contains n space-separated integers representing array A.

The third line contains n space-separated integers representing array B.

outputFormat

For each test case, output one integer on a new line representing the minimum number of operations required to convert array A into array B. If it is impossible, output -1.

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

</p>