#C9251. Array Rotation Problem
Array Rotation Problem
Array Rotation Problem
You are given an array of integers and a non-negative integer k
. Your task is to rotate the array to the right by k
steps, in-place, using O(1) extra space. The rotation means that each element is shifted to the right by k
positions, and the elements that run off the end are wrapped around to the beginning of the array.
For example, if the array is [1, 2, 3, 4, 5, 6, 7]
and k = 3
, then the rotated array is [5, 6, 7, 1, 2, 3, 4]
.
Note: The value of k
can be larger than the length of the array. In such cases, the effective rotation is given by \( k \mod n \), where \( n \) is the number of elements in the array.
inputFormat
The input consists of three lines:
- The first line contains an integer \( n \), denoting the number of elements in the array.
- The second line contains \( n \) space-separated integers representing the elements of the array.
- The third line contains an integer \( k \), indicating the number of steps to rotate the array.
outputFormat
Print the rotated array as a single line of space-separated integers.
## sample7
1 2 3 4 5 6 7
3
5 6 7 1 2 3 4