
Consider the case ‘ninja’array is [ 1, 2, 3, 4 ], ‘enemies’array is [ 2, 1, 4, 5 ] and ‘allowedSwaps’ are = [ [ 0, 1 ], [ 2, 3 ] ] so after swapping in best manner according to ‘allowedSwaps’ our ‘ninja’ array becomes [ 2, 1, 4, 3 ]. So minimum Hamming distance is ‘1’ as now there is only one different element as compared to ‘ninja’ and ‘enemies’ array index.
1. You are allowed to do as many swap operations on the ‘ninja’ array as you want but according to the ‘allowedSwap’ array.
2. You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
The first line of input contains a ‘T’ number of test cases.
The first line of each test case contains an integer ‘n’, which represents the size of the array ‘ninja’ and ‘enemies’.
The second line of each test case contains the ‘n’ space-separated integer of array ‘ninja’.
The third line of each test case contains the ‘n’ space-separated integer of array ‘enemies’.
The fourth line of each test case contains an integer ‘m’ denoting the number of rows in array ‘allowedSwap’. Then, ‘m’ lines follow.
Each line contains two space-separated integers denoting the array values.
For each test case, return the minimum hamming distance of the ‘ninja’ and ‘enemies’ array.
1 <= T <= 100
1 <= N <= 10^3
0 <= ninja[i], enemies[i] < 10^5
0 <= allowedSwaps[i] <=10^5
Where ‘T’ represents the number of test cases, ‘N’ represents the size of the ‘ninja’ and ‘enemies’ array and ninja[i], enemies[i], and allowedSwaps[i] represents the element in the array.
Time Limit: 1 second
The logic used here is, for ex-
‘arr = [1, 2, 3, 4]’
If swap(1, 2) allowed, and swap(1, 4) allowed and we are allowed to do any number of swaps,
then 1, 2, 4 can be rearranged into any possible combination, it forms a SET.
We only need to compare the values of the ‘2’ SET that we find for each array.
The idea here is to use union and try to find all the connected groups (allowedSwaps). In each group find the number of elements that appeared in the ninja array but not in the enemies array.
The logic used here is, for ex-
arr = [1, 2, 3, 4]
If swap(1, 2) allowed, and swap(1, 4) allowed and we are allowed to do any number of swaps,
then 1, 2, 4 can be rearranged into any possible combination, it forms a SET.
We only need to compare the values of the ‘2’ SET that we find for each array.