#K63217. Minimum Swaps to Make Arrays Identical

    ID: 31705 Type: Default 1000ms 256MiB

Minimum Swaps to Make Arrays Identical

Minimum Swaps to Make Arrays Identical

Given two arrays A and B of length n, determine the minimum number of swaps needed to make the two arrays identical. In one swap operation, you can choose a pair of indices i and j (i may be equal to j if needed) in the array A such that swapping the elements at these positions will fix mismatches when compared to array B. More formally, if the arrays are initially identical at many positions and differ at some indices, you can fix a complementary pair of mismatches with one swap. If it is impossible to make the arrays identical (i.e. they do not have the same multiset of elements), return -1.

Note: It is guaranteed that if the arrays contain the same elements with the same frequency, then every swap can fix exactly two mismatches. Mathematically, if we denote by \(d\) the number of indices for which \(A[i] \neq B[i]\), then the answer is \(\lfloor d/2 \rfloor\). Otherwise, the answer is -1.

Example: For \(n = 3\), A = [3, 2, 1] and B = [2, 1, 3], the number of mismatched positions is 3 and one swap is enough, so the answer is 1.

inputFormat

The input is given from standard input (stdin) in the following format:

Line 1: A single integer n, the size of the arrays. Line 2: n space-separated integers, the elements of array A. Line 3: n space-separated integers, the elements of array B.

It is guaranteed that 1 ≤ n ≤ 10^5 and the elements of both arrays are integers.

outputFormat

Output a single integer to standard output (stdout) representing the minimum number of swaps required to make array A identical to array B. If it is impossible, output -1.## sample

3
1 2 3
1 2 3
0