Rotate an Array Right (Set 7)

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

Example 1

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

Why

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

Example 2

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

Why

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