#C6849. Rotate Array

    ID: 50654 Type: Default 1000ms 256MiB

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:

  1. The first line contains an integer n, the number of elements in the array.
  2. The second line contains n space-separated integers representing the elements of the array.
  3. 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.

## sample
7
1 2 3 4 5 6 7
3
5 6 7 1 2 3 4