Rotate an Array Right (Set 8)

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

Example 1

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

Why

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

Example 2

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

Why

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