A circular carousel has n slots, numbered 0 through n-1 running clockwise around a ring, and slot i currently displays the integer label value[i] (labels may be negative, zero, or positive). For every slot i, imagine standing at slot i and walking value[i] steps around the ring: a positive value walks clockwise (the index increases), a negative value walks counter-clockwise (the index decreases), and walking past either end of the ring wraps around to the other end. Record the label that is currently displayed -- using the original array, not any array you are building -- at the slot you land on after that walk.
Produce, for every slot i in order, the label found at the slot reached from i.
The first line contains a single integer n. The second line contains n space-separated integers value[0], value[1], ..., value[n-1].
A single line with n space-separated integers: the label landed on for each slot 0..n-1, in order.
Example 1
Input
5 3 -2 1 4 0
Expected
4 0 4 1 0
Explanation
Slot 0 walks 3 steps clockwise to slot 3, which shows 4. Slot 1 walks 2 steps counter-clockwise, wrapping from slot 1 to slot 4 (label 0). Slot 2 walks 1 step to slot 3 (label 4). Slot 3 walks 4 steps clockwise, wrapping around to slot 2 (label 1). Slot 4 has value 0, so it stays at slot 4 (label 0). The result is 4 0 4 1 0.
Example 2
Input
3 -7 2 -100
Expected
-100 -7 2
Explanation
Slot 0 walks 7 steps counter-clockwise from slot 0 around a ring of size 3, landing on slot 2 (label -100). Slot 1 walks 2 steps clockwise from slot 1, wrapping to slot 0 (label -7). Slot 2 walks 100 steps counter-clockwise from slot 2, which wraps repeatedly around the ring of size 3 and lands on slot 1 (label 2). The result is -100 -7 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →