#C9245. List Right Rotation

    ID: 53317 Type: Default 1000ms 256MiB

List Right Rotation

List Right Rotation

Given a list of elements and an integer ( n ), your task is to rotate the list to the right by ( n ) positions. The rotation operation moves the last ( n ) elements to the front of the list while preserving their order. If ( n ) is greater than the length of the list, it should be taken modulo the length of the list. Similarly, if ( n ) is negative, the rotation will be equivalent to rotating the list to the left by ( |n| ) positions.

For example, given the list [1, 2, 3, 4, 5] and ( n = 2 ), the resulting list after rotation is [4, 5, 1, 2, 3].

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer ( m ) representing the number of elements in the list.
  • The second line contains ( m ) space-separated tokens representing the elements of the list. Each element is a string token.
  • The third line contains an integer ( n ) which is the number of positions to rotate the list to the right.

Note: When ( m = 0 ), the second line will be empty.

outputFormat

Print the rotated list to standard output (stdout) as space-separated tokens in a single line. If the list is empty, print nothing.## sample

5
1 2 3 4 5
2
4 5 1 2 3