Practice arena
C++17·2 s·128 MB
LinearFoundation

Rotate in place

Rotate every array to the right by k positions. Do the transformation in place; k may be larger than n.

Input

  • The first line contains T test cases.
  • Each case contains n and k, followed by one line of n integers.

Output

For each case, print the rotated array on one line.

Technique

three reversals

Target

O(n) time · O(1) extra

Constraints & sample
  • 1 ≤ n ≤ 20,000
  • 0 ≤ k ≤ 10⁹
  • Array values fit in a signed 32-bit integer

Sample input

3
5 2
1 2 3 4 5
4 1
9 8 7 6
1 7
42

Expected output

4 5 1 2 3
6 9 8 7
42
Review the concept

Before you submit

  • Test the smallest valid input and one awkward edge case.
  • Keep the solution within O(n) time · O(1) extra.
  • Match spacing and line breaks exactly before checking 7 hidden cases.
main.cpp
Draft saved locally · 499 / 30,000
Ln 1, Col 1Spaces: 4UTF-8LFC++17

Visible testcases

Editable and stored only in this browser. Hidden judge cases never change.

Checking your session…

Run checks Sample · Submit checks 7 hidden cases

Run the sample first, then send your solution through 7 hidden cases.

Code runs in an isolated Judge0 environment. Drafts and custom visible cases stay on this device; submission metadata and progress sync to MongoDB.