#K55512. Cyclic Shifting of a List

    ID: 29992 Type: Default 1000ms 256MiB

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:

  1. The first line contains an integer n, the number of elements in the list.
  2. The second line contains n space-separated integers representing the list elements. If n is 0, this line may be empty.
  3. 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.

## sample
5
1 2 3 4 5
2
4 5 1 2 3