#C6849. Rotate Array
Rotate Array
Rotate Array
You are given an array of integers and an integer k. Your task is to rotate the array to the right by k steps.
The rotation must be done in-place with O(1) extra space. The algorithm generally involves reversing parts of the array. Specifically, you will first reverse the entire array, then reverse the first k elements, and finally reverse the remaining elements.
The mathematical formulation behind the rotation is defined as follows. For an array \(A\) of size \(n\), after rotating \(k\) times, the new index \(i'\) for an element originally at index \(i\) becomes:
\[ i' = (i + k) \mod n \]inputFormat
The input is read from stdin and consists of three lines:
- The first line contains an integer n, 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, the number of positions to rotate the array.
outputFormat
Output to stdout a single line containing the rotated array. The elements should be printed in order and separated by a single space.
## sample7
1 2 3 4 5 6 7
3
5 6 7 1 2 3 4