Rotate an Array Right (Set 10)

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

Example 1

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

Why

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

Example 2

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

Why

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