Rotate an Array Right (Set 11)

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

Example 1

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

Why

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

Example 2

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

Why

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