Take every 2*i, 2*i+1 index pair as a graph node. Choose one of them, suppose arr[2*i]. Find the node containing arr[2*i] 's pair in final array. In the new node, repeat the process for the adjacent element till we reach arr[2*i+1]. This works because any swap of the items in cycle with outside is unnecessary.

```
#include <bits/stdc++.h>
int minSwaps(vector<int> &arr, int n)
{
vector<int>vis(n, 0); // to check if we have visited a node, total n nodes
vector<int>ind(2*n, 0); // map for value to it index
for(int i = 0; i < 2*n; i++){
ind[arr[i]] = i;
}
int res = 0;
for(int i = 0; i < n; i++){ // kind of dfs
if(vis[i])continue;
vis[i] = 1;
int a = arr[2*i], b = arr[2*i+1];
int cnt = 0, temp = a;
while(temp != b){ // loop till we find a cycle
if(temp%2)temp--; // suppossed final pair
else temp++;
int j = ind[temp]; // final pair's index
if(vis[j/2])break; //check for node thats also a final pair
else vis[j/2] = 1;
if(j%2)j--; // get adj element
else j++;
temp = arr[j]; // move to next node
cnt++; // numberr of swaps = number of edges = number off nodes
}
res+= cnt;
}
return res;
// Write your code here.
}
```