#K6346. Rotate Array In-Place
Rotate Array In-Place
Rotate Array In-Place
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. The rotation must be performed in-place with \(O(1)\) extra space.
In other words, given an array \(nums\) and an integer \(k\), move each element of the array to its new position such that the element which is at index i moves to index \((i+k) \mod n\), where \(n\) is the length of the array. For example, if \(nums = [1, 2, 3, 4, 5, 6]\) and \(k = 2\), the rotated array should be \([5, 6, 1, 2, 3, 4]\).
You are required to read the input from stdin
and output the result to stdout
.
inputFormat
The input consists of two lines:
- The first line contains two integers \(n\) and \(k\), where \(n\) is the number of elements in the array and \(k\) is the number of rotation steps.
- The second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
Output a single line containing the rotated array. The elements should be space-separated.
## sample6 2
1 2 3 4 5 6
5 6 1 2 3 4