#C311. Rotate List
Rotate List
Rotate List
You are given a list of integers and an integer k. Your task is to rotate the list by k positions to the right if k is positive, or to the left if k is negative. The rotation should be performed in-place.
In other words, if the list is \(A = [a_1, a_2, \dots, a_n]\) then after a right rotation by \(k\) positions the list becomes \(A' = [a_{n-k+1}, \dots, a_n, a_1, \dots, a_{n-k}]\) (with an appropriate modulo adjustment if \(k \geq n\)). Similarly, a left rotation by \(|k|\) positions shifts elements to the left.
Example:
- For \(A = [1, 2, 3, 4, 5]\) and \(k = 2\), the rotated list is \([4, 5, 1, 2, 3]\).
- For \(A = [1, 2, 3, 4, 5]\) and \(k = -2\), the rotated list is \([3, 4, 5, 1, 2]\).
Read input from standard input and write the result to standard output.
inputFormat
The input is provided in three lines:
- The first line contains an integer \(n\) (\(0 \leq n \leq 10^5\)), the number of elements in the list.
- The second line contains \(n\) space-separated integers representing the list elements. If \(n = 0\), this line will be empty.
- The third line contains an integer \(k\) (\(|k| \leq 10^9\)), the number of positions to rotate the list.
outputFormat
Output the rotated list as a sequence of space-separated integers in a single line. If the list is empty, output nothing.
## sample5
1 2 3 4 5
2
4 5 1 2 3