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.
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.
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
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.