Introduction
A pointer array is an array L
of nonzero integers where 0 ≤ L[i]+i < len(L)
holds for all indices i
(assuming 0-based indexing).
We say that the index i
points to the index L[i]+i
.
A pointer array is a loop if the indices form a single cycle of length len(L)
.
Here are some examples:
[1,2,-1,3]
is not a pointer array, because the3
does not point to an index.[1,2,-1,-3]
is a pointer array, but not a loop, because no index points to the-1
.[2,2,-2,-2]
is a pointer array, but not a loop, because the indices form two cycles.[2,2,-1,-3]
is a loop.
Input
Your input is a non-empty list of nonzero integers, in any reasonable format. It may be unsorted and/or contain duplicates.
Output
Your output shall be a loop that contains all the integers in the input list (and possibly other integers too), counting multiplicities. They need not occur in the same order as in the input, and the output need not be minimal in any sense.
Example
For the input [2,-4,2]
, an acceptable output would be [2,2,-1,1,-4]
.
Rules and scoring
You can write a full program or a function. The lowest byte count wins, and standard loopholes are disallowed. Including a couple of example inputs and outputs in your answer is appreciated.
Test cases
These are given in the format input -> some possible output(s)
.
[1] -> [1,-1] or [1,1,1,-3]
[2] -> [2,-1,-1] or [1,2,-2,-1]
[-2] -> [1,1,-2] or [3,1,2,-2,-4]
[2,-2] -> [2,-1,1,-2] or [2,-1,2,-2,-1]
[2,2,2] -> [2,-1,2,-2,2,-2,-1] or [2,2,2,2,-3,-5]
[2,-4,2] -> [2,2,-1,1,-4] or [2,5,1,1,1,-4,2,-7,-1]
[3,-1,2,-2,-1,-5] -> [2,3,-1,2,-1,-5] or [3,3,-1,-1,2,2,-1,6,1,1,1,1,-12,-5]
[-2,-2,10,-2,-2,-2] -> [10,-1,1,-2,-2,1,-2,-2,1,-2,-2]
[-15,15,-15] -> [15,-1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,-15,-15]
[1,2,3,4,5] -> [1,2,3,-1,4,-1,5,-1,-1,-9,-1,-1]