#C1488. Right Rotation of an Array
Right Rotation of an Array
Right Rotation of an Array
You are given an array of integers and a non-negative integer k. Your task is to rotate the array to the right by k steps. That is, every element of the array is shifted to the right by k positions and the elements at the end of the array wrap around to the beginning. Formally, if the array has n elements, then each element at index i moves to index \((i+k) \bmod n\).
For example, if A = [1, 2, 3, 4, 5]
and k = 2
, then the rotated array is [4, 5, 1, 2, 3]
.
Your solution should read the input from standard input (stdin) and output the rotated array to standard output (stdout) with each element separated by a single space.
inputFormat
The input consists of three parts:
- An integer n on the first line, representing the number of elements in the array.
- If n > 0, the second line contains n space-separated integers representing the array elements. If n = 0, this line is omitted.
- An integer k on the next line, representing the number of positions to rotate the array to the right.
Note: When n = 0, the array is empty.
outputFormat
Output the rotated array as a sequence of space-separated integers in one line. If the array is empty, output nothing.
## sample5
1 2 3 4 5
0
1 2 3 4 5