Rotate an Array Right (Set 14)

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

Example 1

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

Why

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

Example 2

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

Why

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