#C14285. Rotate a List Right by k Positions
Rotate a List Right by k Positions
Rotate a List Right by k Positions
You are given a list of integers and an integer k. Your task is to rotate the list to the right by k positions. That is, each element in the list should be moved to a new position calculated as follows:
\(\text{newIndex} = (i + k) \mod n\), where \(n\) is the number of elements in the list and \(i\) is the original index. The rotation should work for both positive and negative values of k (with negative values implying left rotation). The function should perform the rotation in place and output the resulting list.
Note: If the list is empty, simply output an empty list. If k is greater than the length of the list or negative, use the formula \(k \mod n\) to determine the effective rotation.
inputFormat
The input is read from standard input (stdin) and consists of the following:
- An integer n denoting the number of elements in the list.
- A line containing n space-separated integers representing the list.
- An integer k denoting the number of positions to rotate the list.
outputFormat
The output should be written to standard output (stdout) and consists of a single line with the elements of the rotated list, space-separated.
## sample5
1 2 3 4 5
2
4 5 1 2 3