n cells in a row, numbered 0 to n - 1. Cell i holds a note with a cell number links[i]. All the notes are different, so every cell is mentioned exactly once.
For every cell i, work out where you end up after two hops: first follow the note in i, then follow the note in the cell you landed on. Return a list of length n whose entry i is the cell you finish in.
Bonus: can you do it with no extra memory by writing the answer back into links?links=[2, 0, 1] → [1, 2, 0]
From 0 we go to 2, then to 1; from 1 to 0, then to 2; from 2 to 1, then to 0. That gives [1, 2, 0].
links=[1, 0, 3, 2] → [0, 1, 2, 3]
The cells form pairs pointing at each other, so two hops always bring you back to where you started.
links=[0] → [0]
links are distinctlinks=[2, 0, 1]
[1, 2, 0]
Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.