Consider the following problem:
Suppose we have two arrays containing the same distinct elements, and we want to transform one into the other. We are allowed to swap any two positions. What is the minimum number of swaps we need to use?
To give an example, consider the following arrays:
nums = [3, 1, 2, 5, 4]
target = [1, 2, 3, 4, 5]
The minimum number of swaps required for turning nums into target is 3.
Give the problem a try before reading on! The code ends up being short, but the reason it works is a neat idea from discrete mathematics: permutation cycles.
Permutations as functions
I was first introduced to the idea of permutations as functions by the following definition:
A permutation is a bijection from some set back to the same set, that is, a function $f\colon A \to A$ that is a bijection. Some elements of $A$ may be mapped to themselves and some perhaps to other elements. A permutation can thus describe a kind of rearrangement of a set.
For example, on the set $\lbrace 1, 2, 3, 4, 5, 6 \rbrace$ we can define a permutation by:
$$ \pi(1) = 2, \quad \pi(2) = 3, \quad \pi(3) = 1, \quad \pi(4) = 6, \quad \pi(5) = 5, \quad \pi(6) = 4 $$
Rather than writing out every function value, we often use special notation to represent permutations. There are a few different ways of writing permutations, but I'll go through the two-line notation and the cycle notation here, because they are the most relevant.
In two-line notation one writes the elements of the set being permuted on the top row in some order. On the bottom row one writes the element to which each top-row element is mapped. So for the permutation $\pi$ above, we can write:
$$ \pi = \begin{pmatrix} 1 & 2 & 3 & 4 & 5 & 6 \cr 2 & 3 & 1 & 6 & 5 & 4 \end{pmatrix} $$
Notice how there are cycles in the permutation above. We can see that going $1 \to 2 \to 3 \to 1$ leads back to 1. Similarly $4 \to 6 \to 4$, and 5 is mapped to itself. A more dynamic view of how a permutation acts is given by cycle notation, where each cycle is written in its own parentheses:
$$ \pi = (1\ 2\ 3)(4\ 6)(5) $$
So how does this help us? Let's check out the example again:
nums = [3, 1, 2, 5, 4]
target = [1, 2, 3, 4, 5]
See how this looks a lot like the two-line notation of permutations, with nums as the top row and target as the bottom row!
Let $A$ be the set of elements in the arrays, and define $\sigma\colon A \to A$ by $\sigma(\texttt{nums[i]}) = \texttt{target[i]}$. Since both arrays contain the same distinct elements, $\sigma$ is a bijection from $A$ to itself. Following $3 \to 1 \to 2 \to 3$ and $5 \to 4 \to 5$, we can write it in cycle notation:
$$ \sigma = (3\ 1\ 2)(5\ 4) $$
Why are cycles important? Well, notice how we now have a very straightforward way of understanding how many swaps we need. In the case of the second cycle $(5\ 4)$, only 1 swap is needed to get both of the elements into the correct position. For longer cycles, think about it like this: we can always swap so that one element lands in its correct place, so when we do the last swap, the last element has to land in its correct position too. A cycle of length $k$ therefore needs $k - 1$ swaps.
To derive a more general formula, suppose our permutation contains $c$ disjoint cycles (counting elements that are already in the right place as cycles of length 1) with lengths
$$ k_1, k_2, \ldots, k_c. $$
Since every element belongs to exactly one cycle, the lengths add up to the number of elements $n$:
$$ k_1 + k_2 + \cdots + k_c = n. $$
Each cycle requires $k_i - 1$ swaps, so the total is
$$ \begin{aligned} S &= (k_1 - 1) + (k_2 - 1) + \cdots + (k_c - 1) \cr &= (k_1 + k_2 + \cdots + k_c) - c \cr &= \boxed{n - c}. \end{aligned} $$
Therefore we end up with a simple formula for the number of swaps this strategy needs.
Can we do better?
Suppose we swap two elements $a$ and $b$. Then $a$ is mapped to where $b$ used to go, and vice versa: $a \to \sigma(b)$ and $b \to \sigma(a)$. If $a$ and $b$ were in the same cycle, this splits the cycle into two. If they were in different cycles, this merges them into one. Either way, a swap changes the number of cycles by exactly one.
When nums equals target, $\sigma$ is the identity permutation, where every element is its own cycle of length 1, so it has $n$ cycles. Starting from $c$ cycles and gaining at most one per swap, we need at least $n - c$ swaps.
Since we already know that $n - c$ swaps are enough, the answer is exactly $n - c$. In our example, $n = 5$ and $c = 2$, giving $3$ swaps.
In code
All we need to do is count the cycles:
def min_swaps(nums, target):
# Map each value to the value that should replace it
mapping = dict(zip(nums, target))
visited = set()
cycles = 0
for num in nums:
if num in visited:
continue
# Found a new cycle
cycles += 1
current = num
# Follow the cycle until we return to a visited element
while current not in visited:
visited.add(current)
current = mapping[current]
return len(nums) - cycles
The time complexity of this solution is $O(n)$!
Casimir Rönnlöf, October 2026