#K95527. Array Right Rotation
Array Right Rotation
Array Right Rotation
You are given an array of integers and an integer \( k \). Your task is to rotate the array to the right by \( k \) positions. In other words, the last \( k \) elements of the array should move to the front and the rest should shift to the right.
Formally, given an array \( A \) of length \( n \) and an integer \( k \), the rotated array is defined as:
[ A' = [A_{(n-k \bmod n)}, \ldots, A_{n-1}, A_{0}, \ldots, A_{(n-k \bmod n)-1}] ]
Note: Use \( k \bmod n \) to handle cases where \( k \) is greater than or equal to \( n \).
For example, if \( A = [1, 2, 3, 4, 5] \) and \( k = 2 \), the output should be [4, 5, 1, 2, 3]
.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains two integers:
n
(the size of the array) andk
(the number of positions to rotate). - The second line contains
n
space-separated integers representing the elements of the array.
outputFormat
Print the rotated array as space-separated integers on a single line to stdout.
## sample5 2
1 2 3 4 5
4 5 1 2 3