#C7795. Rotate Array

    ID: 51705 Type: Default 1000ms 256MiB

Rotate Array

Rotate 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 ) positions, i.e., move every element of the array ( k ) positions to the right, wrapping around to the beginning of the array if necessary.

Formally, for an array ( A = [a_0, a_1, \ldots, a_{n-1}] ), rotating the array by ( k ) positions (where ( k ) may be greater than ( n )) results in the array ( [a_{(n-k % n)}, a_{(n-k % n)+1}, \ldots, a_{n-1}, a_0, a_1, \ldots, a_{(n-k % n)-1}] ).

For example, if ( n = 5 ), ( A = [1, 2, 3, 4, 5] ), and ( k = 2 ), then the rotated array is ( [4, 5, 1, 2, 3] ).

inputFormat

The input is given in the following format:

( n ): An integer representing the number of elements in the array (first line).
Next, a line with ( n ) space-separated integers representing the array elements.
Finally, a line containing an integer ( k ) representing the number of positions to rotate the array to the right.

outputFormat

Output the rotated array as a single line of ( n ) space-separated integers. If the array is empty, output an empty line.## sample

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