A freight train's containers form a singly linked list, front to back. Rotate the container order to the RIGHT by k positions: the last k containers move to the front (preserving their relative order), and the remaining containers follow after them (also preserving their relative order). k may be larger than the number of containers n; only the effective rotation (k mod n) matters. Print the resulting order.
Line 1: an integer n — the number of containers.
Line 2: n space-separated integers — the container weights, front to back.
Line 3: an integer k — the rotation amount.
n space-separated integers: the weights after rotating right by k (mod n) positions.
Example 1
Input
5 1 2 3 4 5 2
Expected
4 5 1 2 3
Explanation
The last 2 values (4,5) move to the front: 4 5 1 2 3.
Example 2
Input
4 10 20 30 40 6
Expected
30 40 10 20
Explanation
Effective rotation is 6 mod 4 = 2, so the last 2 values (30,40) move to the front: 30 40 10 20.
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 →