#K68047. Array Rotation

    ID: 32777 Type: Default 1000ms 256MiB

Array Rotation

Array Rotation

Given an array of integers and an integer k, rotate the array to the right by k steps. This means that the last k elements of the array are moved to the beginning in the same order. The rotation must be performed in-place with O(1) extra space. Mathematically, the new index for any element originally at index \(i\) is given by:

$$ \text{new_index} = (i + k) \mod n $$

where \(n\) is the number of elements in the array.

Your task is to implement this rotation and output the updated array.

inputFormat

The input 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 array.
  • The third line contains an integer k, the number of steps to rotate the array.

outputFormat

Output the rotated array as one line of space-separated integers.

## sample
5
1 2 3 4 5
2
4 5 1 2 3