#K55512. Cyclic Shifting of a List
Cyclic Shifting of a List
Cyclic Shifting of a List
You are given a list of integers and an integer k. Your task is to cyclically shift the list by k positions. If k is positive, shift the list to the right; if k is negative, shift the list to the left by |k|
positions.
Mathematically, the effective shift is given by \( k \mod n \) where \( n \) is the length of the list. If the list is empty, simply output an empty list.
Examples:
- For L = [1, 2, 3, 4, 5] and k = 2, the output should be [4, 5, 1, 2, 3].
- For L = [1, 2, 3, 4, 5] and k = -1, the output should be [2, 3, 4, 5, 1].
inputFormat
The input is given from stdin and consists of three lines:
- The first line contains an integer n, the number of elements in the list.
- The second line contains n space-separated integers representing the list elements. If n is 0, this line may be empty.
- The third line contains an integer k, the number of positions to shift.
outputFormat
Output the shifted list to stdout as a sequence of space-separated integers in a single line. If the list is empty, output nothing.
## sample5
1 2 3 4 5
2
4 5 1 2 3