Rotate an Array Right (Set 13)

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 13) — the data. This set runs 4 cases: nums holds arrays of 48–48 items, with values from -20 to 20; k ranges over 0–38. The hidden cases are the largest of the set.

Example 1

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

Why

`rotateArrayRight` returns `[10, 14, -9, 20, -8, -14, 8, 14, -20, 13, -7, 4, 10, 11, 2, 5, 2, 13, -8, 15, 2, 0, -19, -6, 0, -11, 3, -8, 15, -18, 4, 6, -8, -2, 3, -11, 0, 1, -18, -1, -13, 4, -8, 12, -16, -5, -14, -19]` for this input.

Example 2

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

Why

`rotateArrayRight` returns `[19, 19, -20, 7, -11, 4, 15, 8, -10, 11, 6, 1, -9, -10, -16, -15, -3, -14, 18, 14, 20, 15, -11, 17, -12, 15, -17, 20, -18, -19, 0, -11, 14, -14, 3, -6, 6, 14, -16, 4, 5, 2, -6, -12, -20, -15, 7, -4]` 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.