You are viewing a single comment's thread. Return to all comments →
Please modify your solution like the following to avoid index out of bound
int minimumSwaps(vector<int> arr) { int i,c=0,n=arr.size(); for(i=0;i<n;i++) { if(arr[i]==(i+1)) continue; if(arr[i]-1>=0 && arr[i]-1<arr.Length) { swap(ref arr[i],ref arr[arr[i]-1]); c++; i--; } } return c; }
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
Please modify your solution like the following to avoid index out of bound