#K34537. Rotate Array
Rotate Array
Rotate Array
Given an array of integers and an integer ( k ), rotate the array to the right by ( k ) steps. More formally, for an array ( A ) of length ( n ) and an integer ( k ), each element ( A[i] ) is moved to position ( (i + k) % n ) (using 0-indexing). Note that if ( k ) is negative, the array is rotated to the left by ( |k| ) steps. The rotation must be performed in a way that handles cases where ( k ) is greater than ( n ) as well as when the array is empty.
For example, if ( A = [1,2,3,4,5,6,7] ) and ( k = 3 ), then the new order of the array will be ( [5,6,7,1,2,3,4] ).
inputFormat
The input is read from standard input (stdin) and consists of multiple lines:
- The first line contains an integer ( n ) representing the number of elements in the array.
- The second line contains ( n ) space-separated integers representing the elements of the array. If ( n ) is zero, this line will be empty.
- The third line contains an integer ( k ), which is the number of steps to rotate the array.
outputFormat
Print the rotated array as space-separated integers on a single line to standard output (stdout). If the array is empty, output nothing.## sample
7
1 2 3 4 5 6 7
3
5 6 7 1 2 3 4