Rotate an Array Right (Set 6)

Medium

Given an array of integers nums and a whole number k, rotate the array to the right by k positions and return the result.

k may be larger than the array; rotating by nums.length is a no-op.

Rotate an Array Right (Set 6) — the data. This set runs 4 cases: nums holds arrays of 22–44 items, with values from -20 to 20; k ranges over 3–14. The hidden cases are the largest of the set.

Example 1

rotateArrayRight()
Input
nums = [-10, 4, 10, 4, 6, 5, -20, -17, -3, 18, -7, 12, -5, 5, -14, -20, -6, -11, 11, 0, 1, -18], k = 12
Output
[-7, 12, -5, 5, -14, -20, -6, -11, 11, 0, 1, -18, -10, 4, 10, 4, 6, 5, -20, -17, -3, 18]

Why

`rotateArrayRight` returns `[-7, 12, -5, 5, -14, -20, -6, -11, 11, 0, 1, -18, -10, 4, 10, 4, 6, 5, -20, -17, -3, 18]` for this input.

Example 2

rotateArrayRight()
Input
nums = [-9, 11, -17, 4, 19, -20, -4, -11, 2, -15, -2, 20, -14, 14, 17, -12, -18, 17, -12, 9, 18, -10, 13, 9, -15, 7, -20, 10, 0, 1, 5, -3, 1, -19], k = 4
Output
[5, -3, 1, -19, -9, 11, -17, 4, 19, -20, -4, -11, 2, -15, -2, 20, -14, 14, 17, -12, -18, 17, -12, 9, 18, -10, 13, 9, -15, 7, -20, 10, 0, 1]

Why

`rotateArrayRight` returns `[5, -3, 1, -19, -9, 11, -17, 4, 19, -20, -4, -11, 2, -15, -2, 20, -14, 14, 17, -12, -18, 17, -12, 9, 18, -10, 13, 9, -15, 7, -20, 10, 0, 1]` for this input.
Constraints
0 <= nums.length <= 10^4
0 <= k <= 10^6
Every case in this set must pass: the first two are visible on Run, the last two unlock on Submit.

Topics

ArraysTwo Pointers

Implement rotateArrayRight in the editor and return the answer — printing it is never graded.