# Given an array with N elements and an array of N indices find the lowest possible amounts of swaps [duplicate]

So I have two arrays $$d$$ and $$s$$ with size $$n \in \mathbb{N}$$. Array $$d$$ contains the elements and array $$s$$ contains indices. Array $$s$$ contains all numbers from $$0$$ upto $$n$$ with no duplicates.

The indices in $$s$$ indicate where the elements in $$d$$ need to go. So the element at $$d[q]$$ needs to be at $$d[s[q]]$$ where $$n > q \geq 0$$.

The goal is to find the minimum amount of $$q \in \mathbb{N}$$ swaps such that the after all swaps are performed the above holds.

• Does this answer your question? Sort array with minimum swaps. You can just stipulate the desired array $d[s[0]], d[s[1]], \cdots$ is in increasing order. – John L. Jan 27 at 21:34