Suppose we have an array \$\texttt{ps}\$ of length \$n\$ with pointers pointing to some location in the array: The process of "pointer jumping" will set every pointer to the location the pointer it points to points to.
For the purpose of this challenge a pointer is the (zero-based) index of an element of the array, this implies that every element in the array will be greater or equal to \$0\$ and less than \$n\$. Using this notation the process can be formulated as follows:
for i = 0..(n-1) {
ps[i] = ps[ps[i]]
}
This means (for this challenge) that the pointers are updated in-place in sequential order (ie. lower indices first).
Example
Let's work through an example, \$\texttt{ps = [2,1,4,1,3,2]}\$:
$$ \texttt{i = 0}: \text{the element at position }\texttt{ps[0] = 2}\text{ points to }\texttt{4} \\ \to \texttt{ps = [4,1,4,1,3,2]} \\ \texttt{i = 1}: \text{the element at position }\texttt{ps[1] = 1}\text{ points to }\texttt{1} \\ \to \texttt{ps = [4,1,4,1,3,2]} \\ \texttt{i = 2}: \text{the element at position }\texttt{ps[2] = 4}\text{ points to }\texttt{3} \\ \to \texttt{ps = [4,1,3,1,3,2]} \\ \texttt{i = 3}: \text{the element at position }\texttt{ps[3] = 1}\text{ points to }\texttt{1} \\ \to \texttt{ps = [4,1,3,1,3,2]} \\ \texttt{i = 4}: \text{the element at position }\texttt{ps[4] = 3}\text{ points to }\texttt{1} \\ \to \texttt{ps = [4,1,3,1,1,2]} \\ \texttt{i = 5}: \text{the element at position }\texttt{ps[5] = 2}\text{ points to }\texttt{3} \\ \to \texttt{ps = [4,1,3,1,1,3]} $$
So after one iteration of "pointer jumping" we get the array \$\texttt{[4,1,3,1,1,3]}\$.
Challenge
Given an array with indices output the array obtained by iterating the above described pointer jumping until the array does not change anymore.
Rules
Your program/function will take and return/output the same type, a list/vector/array etc. which
- is guaranteed to be non-empty and
- is guaranteed to only contain entries \$0 \leq p < n\$.
Variants: You may choose
- to use 1-based indexing or
- use actual pointers,
however you should mention this in your submission.
Test cases
[0] → [0]
[1,0] → [0,0]
[1,2,3,4,0] → [2,2,2,2,2]
[0,1,1,1,0,3] → [0,1,1,1,0,1]
[4,1,3,0,3,2] → [3,1,3,3,3,3]
[5,1,2,0,4,5,6] → [5,1,2,5,4,5,6]
[9,9,9,2,5,4,4,5,8,1,0,0] → [1,1,1,1,4,4,4,4,8,1,1,1]
n
as additional input? \$\endgroup\$#[[#]]&~FixedPoint~#&
. \$\endgroup\$