#K7241. Array Rotation
Array Rotation
Array Rotation
Given an array, an integer \(k\), and a rotation direction (either left
or right
), rotate the array accordingly.
For a left rotation, the resulting array \(a_{new}\) is defined as: \(a_{new}[i] = a[(i+k) \bmod n]\). For a right rotation, the transformation is: \(a_{new}[i] = a[(i-k+n) \bmod n]\), where \(n\) is the number of elements in the array.
Your task is to perform the rotation and output the resulting array.
inputFormat
The input is provided via stdin and has the following format:
- The first line contains an integer \(n\), representing the number of elements in the array.
- The second line contains \(n\) space-separated integers (if \(n > 0\)); if \(n = 0\), this line will be empty.
- The third line contains an integer \(k\), the number of rotations.
- The fourth line contains a string which is either
left
orright
, indicating the direction of rotation.
outputFormat
Print the rotated array to stdout as a single line of space-separated integers. If the array is empty, print nothing.
## sample8
1 2 3 4 5 6 7 8
3
left
4 5 6 7 8 1 2 3