Problem of the day
You are given two arrays, ‘A’ and ‘B’, with the same elements in a different order. Your task is to make ‘B’ identical to the ‘A’ by swapping the elements in array ‘B’. You have to find out the minimum number of swaps required.
For Example:For the first test case, ‘A’ = [5, 6, 4, 10], ‘B’ = [4, 6, 10, 5], here in the ‘B’ we can swap 4 with 10 to form [10, 6, 4, 5] and we can then swap 10 with 5 to form the array [5, 6, 4, 10] which is identical to ‘A’. Hence the answer is 2.
The first input line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ representing the size of both arrays.
The second line of each test case contains ‘N’ space-separated integers representing the ‘A’ array elements.
The third line of each test case contains ‘N’ space-separated integers representing the ‘B’ array elements.
Output Format:
For each test case, print a single integer representing the minimum number of swaps required to make ‘B’ identical to ‘A’
Print a separate line for each test case.
1 ≤ T ≤ 10
1 ≤ N ≤ 1000
0 ≤ A[i], B[i] ≤ 10^9
Time Limit: 1 sec
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
2
4
5 6 4 10
4 6 10 5
3
1 2 3
1 3 2
2
1
For the first test case, ‘A’ = [5, 6, 4, 10], ‘B’ = [4, 6, 10, 5], here in the ‘B’ we can swap 4 with 10 to form [10, 6, 4, 5] and we can then swap 10 with 5 to form the array [5, 6, 4, 10] which is identical to ‘A’. Hence the answer is 2.
For the second test case, ‘A’ = [1, 2, 3], ‘B’ = [1, 3, 2], here in the second array we can swap 3 with 2 to form [1, 2, 3] which is identical to ‘A’. Hence the answer is 1.
2
5
1 2 3 4 0
0 4 3 2 1
4
1 2 3 4
1 2 3 4
2
0
Try Storing the indices of all the elements in the first array.
In this approach, we will try to store the indices of all the elements in the first array in the second array. Suppose the ith element in the first array is at jth position in the second array. In that case, we store B[i] = j, now we know the elements that should be at ith index in B is actually at B[i]th position.
So we have an array of current positions of elements at each index. If we sort this array with a minimum number of swaps, we would have put all the elements correctly. For example: if A = [5, 6, 4, 10] and B = [4, 6, 10, 5], after putting the indices we will have B = [2, 1, 3, 0], if we sort this array will put all the elements in positions they were in A.
We can count minimum swaps to sort the array by making the array into a graph where we will put an edge from the current position to the final position in the array and count the cycles in the graph.
To count the minimum number of swaps to sort the array, we will create a function swapsToSort(arr)
Algorithm:
O( N * log( N ) ), where N is the length of the array.
We are iterating over the array, costing O(N) time. To sort the position array will cost O(N*log(N)) time.
Hence the overall time complexity is O(N*log(N)).
O( N ), where N is the length of the array.
We maintain a map of positions and an array of pairs that will cost O(N) space each.
Hence the overall space complexity is O(N)