Given two arrays which have same values but in different order, we need to make second array same as first array using minimum number of swaps.

Examples:

Input : arrA[] = {3, 6, 4, 8}, arrB[] = {4, 6, 8, 3} Output : 2 we can make arrB to same as arrA in2swaps which are shown below, swap 4 with 8, arrB = {8, 6, 4, 3} swap 8 with 3, arrB = {3, 6, 4, 8}

This problem can be solved by modifying the array B. We save the index of array A elements in array B i.e. if ith element of array A is at jth position in array B, then we will make arrB[i] = j

For above given example, modified array B will be, arrB = {3, 1, 0, 2}. This modified array represents distribution of array A element in array B and our goal is to sort this modified array in minimum number of swaps because after sorting only array B element will be aligned with array A elements.

Now count of minimum swaps for sorting an array can be found by visualizing the problem as a graph, this problem is already explained in previous article.

So we count these swaps in modified array and that will be our final answer.

Please see below code for better understanding.

`// C++ program to make an array same to another ` `// using minimum number of swap ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function returns the minimum number of swaps ` `// required to sort the array ` `// This method is taken from below post ` `int` `minSwapsToSort(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Create an array of pairs where first ` ` ` `// element is array element and second element ` ` ` `// is position of first element ` ` ` `pair<` `int` `, ` `int` `> arrPos[n]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `arrPos[i].first = arr[i]; ` ` ` `arrPos[i].second = i; ` ` ` `} ` ` ` ` ` `// Sort the array by array element values to ` ` ` `// get right position of every element as second ` ` ` `// element of pair. ` ` ` `sort(arrPos, arrPos + n); ` ` ` ` ` `// To keep track of visited elements. Initialize ` ` ` `// all elements as not visited or false. ` ` ` `vector<` `bool` `> vis(n, ` `false` `); ` ` ` ` ` `// Initialize result ` ` ` `int` `ans = 0; ` ` ` ` ` `// Traverse array elements ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// already swapped and corrected or ` ` ` `// already present at correct pos ` ` ` `if` `(vis[i] || arrPos[i].second == i) ` ` ` `continue` `; ` ` ` ` ` `// find out the number of node in ` ` ` `// this cycle and add in ans ` ` ` `int` `cycle_size = 0; ` ` ` `int` `j = i; ` ` ` `while` `(!vis[j]) ` ` ` `{ ` ` ` `vis[j] = 1; ` ` ` ` ` `// move to next node ` ` ` `j = arrPos[j].second; ` ` ` `cycle_size++; ` ` ` `} ` ` ` ` ` `// Update answer by adding current cycle. ` ` ` `ans += (cycle_size - 1); ` ` ` `} ` ` ` ` ` `// Return result ` ` ` `return` `ans; ` `} ` ` ` `// method returns minimum number of swap to make ` `// array B same as array A ` `int` `minSwapToMakeArraySame(` `int` `a[], ` `int` `b[], ` `int` `n) ` `{ ` ` ` `// map to store position of elements in array B ` ` ` `// we basically store element to index mapping. ` ` ` `map<` `int` `, ` `int` `> mp; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `mp[b[i]] = i; ` ` ` ` ` `// now we're storing position of array A elements ` ` ` `// in array B. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `b[i] = mp[a[i]]; ` ` ` ` ` `/* We can uncomment this section to print modified ` ` ` `b array ` ` ` `for (int i = 0; i < N; i++) ` ` ` `cout << b[i] << " "; ` ` ` `cout << endl; */` ` ` ` ` `// returing minimum swap for sorting in modified ` ` ` `// array B as final answer ` ` ` `return` `minSwapsToSort(b, n); ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `a[] = {3, 6, 4, 8}; ` ` ` `int` `b[] = {4, 6, 8, 3}; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(` `int` `); ` ` ` `cout << minSwapToMakeArraySame(a, b, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

2

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum changes required to make two arrays identical
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Check whether array has all identical elements using Arrays.asList() and HashSet in Java
- Minimum Swaps required to group all 1's together
- Minimum swaps so that binary search can be applied
- Minimum number of swaps required to sort an array | Set 2
- Minimum number of swaps required to sort an array
- Minimum swaps required to bring all elements less than or equal to k together
- Minimum adjacent swaps required to Sort Binary array
- Minimum number of swaps required for arranging pairs adjacent to each other
- Minimum number of adjacent swaps for arranging similar elements together
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of swaps required to sort an array of first N number
- Check whether it is possible to make both arrays equal by modifying a single element
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- Minimum removals to make array sum even
- Minimum operations to make the MEX of the given set equal to x
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Minimum removals from array to make max - min <= K