Rotate an Array Right (Set 12)

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

Example 1

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

Why

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

Example 2

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

Why

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