We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Minimum Swaps 2
- Discussions
Minimum Swaps 2
Minimum Swaps 2
Sort by
recency
|
2473 Discussions
|
Please Login in order to post a comment
simple python code. if u use for loop with this logic it won't work smh.
c++ code with simple concepts
" bool checkZero(int num1 , int num2){ if(num1 - num2 == 1){ return true; } return false; } // Complete the minimumSwaps function below. int minimumSwaps(vector arr) { int size = arr.size();
} "
I noticed that in Julia the code raises error for STDIN written with capital letters. Once I changed it to stdin, everything went smooth. So, I guess you should update the current version (unless I am missing something).
Python code.
Final ordered array should look like [1, 2, 3, ...], so we can iterate through the array and check if current value matches index+1. If they do not match, find the index of the value that matches index+1. Then swap the values at the current index and the other index and record the swap.
Note: this code wouldn't work if the array isn't consecutive but it seems like all the test case arrays are consecutive now.