#C10147. Rotate Array

    ID: 39320 Type: Default 1000ms 256MiB

Rotate Array

Rotate Array

You are given an integer array and an integer k. Your task is to rotate the array to the right by k steps. The rotation is performed in such a way that the last k elements of the array become the first k elements of the rotated array.

The operation can be mathematically described by the formula:

[ \text{rotated}[i] = \begin{cases} \text{arr}[n-k+i] & \text{if } 1 \leq i \leq k,\ \text{arr}[i-k] & \text{if } k+1 \leq i \leq n,\ \end{cases} ]

where n is the number of elements in the array. You need to read the input from stdin and output the result to stdout.

inputFormat

The input consists of three lines:

  1. The first line contains an integer n which denotes 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 indicating the number of positions to rotate the array to the right.

outputFormat

Output the rotated array as space-separated integers in a single line. There should be no extra spaces at the end of the line.## sample

7
1 2 3 4 5 6 7
3
5 6 7 1 2 3 4