#K13726. Rotate Array
Rotate Array
Rotate Array
You are given an array of n integers and an integer k. Your task is to rotate the array to the right by k steps. In each rotation, every element is shifted one position to the right, and the last element of the array moves to the beginning.
For example, if the array is given as:
- n = 7, array = [1, 2, 3, 4, 5, 6, 7], and k = 3
After rotation, the array becomes:
- [5, 6, 7, 1, 2, 3, 4]
Note: If k is greater than the size of the array, rotate it by k mod n steps.
Implement the function such that the input is read from standard input (stdin
) and the output (rotated array) is printed to standard output (stdout
) as space-separated integers on a single line.
inputFormat
The input will be provided in 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.
- The third line contains an integer k which is the number of rotations.
outputFormat
Output the rotated array as a single line with space-separated integers.
## sample7
1 2 3 4 5 6 7
3
5 6 7 1 2 3 4