#C7324. Minimum Adjacent Swaps to Transform an Array
Minimum Adjacent Swaps to Transform an Array
Minimum Adjacent Swaps to Transform an Array
In this problem, you are given two arrays (a) and (b), each containing (n) integers. Your task is to determine the minimum number of adjacent swap operations required to transform array (a) into array (b). An adjacent swap operation consists of swapping (a[i]) with (a[i+1]) for some valid index (i). If it is not possible to transform (a) into (b) (i.e. the two arrays have different multisets of elements), you should return (-1).
Note: The transformation is performed by simulating the adjacent swaps in (a) to match the order of (b).
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
The first line contains a single integer (T) representing the number of test cases. Each test case is described as follows:
- The first line contains an integer \(n\) representing the number of elements in the arrays.
- The second line contains \(n\) space-separated integers representing the array \(a\).
- The third line contains \(n\) space-separated integers representing the array \(b\).
outputFormat
For each test case, print an integer on a new line representing the minimum number of adjacent swap operations needed to transform array (a) into array (b). If the transformation is not possible, print (-1). The output is written to standard output (stdout).## sample
4
4
1 3 2 4
3 1 2 4
3
2 1 3
1 2 3
4
4 3 2 1
1 2 3 4
3
1 2 3
1 2 2
1
1
6
-1
</p>