this is the extended version of minimum swaps to sort the array . in this we have to find the length of cycle present and add into the answer

#include <bits/stdc++.h>

int minimumSwaps(vector<int>& A, vector<int>& B){

// Write your code here.

unordered_map<int,int>m;

for(int i=0;i<A.size();i++){

m[A[i]]=i;

}

// above map is holding the actual array that B has to //finally look like

int ans=0;

vector<int>vis(B.size(),0);

for(int i=0;i<B.size();i++){

if(vis[i] || i==m[B[i]])continue; // if an index is visited

// if an element is at his correct posn then don't do anything

int c=0;

int j=i;

while(vis[j]==false){

vis[j]=true;

j=m[B[j]]; // detecting cycle

c++;

//cout<<c<<"\n";

}

ans+=c-1; // if length of cycle is 4 then 3 swaps needed

}

return ans;

}