JavaMediumArrays20 min

Rotate an array to the right

Print the result of rotating right by k positions, space-separated; empty arrays produce an empty line. Follow the stated ordering and edge-case conventions exactly. Interviewers use this task to probe algorithm selection, invariants, and boundary handling.

Input

n, n integers, then nonnegative k.

Output

Print the result of rotating right by k positions, space-separated; empty arrays produce an empty line.

Constraints

0 <= n <= 100000; 0 <= k <= 1000000000; values in [-1000000000,1000000000].

Example

Input

5 1 2 3 4 5 2

Output

4 5 1 2 3

Example 2 input

4 -1 0 1 2 1000000000

Output

-1 0 1 2

This example exercises a further boundary or ordering case.

Public test cases (2)

  • Input

    5 1 2 3 4 5 2

    Expected output

    4 5 1 2 3
  • Input

    3 1 2 3 4

    Expected output

    3 1 2
Hints (2)
  1. Identify the state needed to avoid recomputing work for each element.
  2. Normalize k by n and map each output position to its original index.

Target complexity: time O(n) · space O(n)

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All Java programs