Rotate an Array Right (Set 4)

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

Example 1

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

Why

`rotateArrayRight` returns `[-9, 16, 11, -6, 10, 15, -18, -15, -17, -12, -15, 4, 5, -20, 16, -20, 6, -15, -8, 10, -3]` for this input.

Example 2

rotateArrayRight()
Input
nums = [18, -19, -6, -5, 17, -17, -20, 17, -2, -13, 4, -4, 1, 20, -3, 15], k = 12
Output
[17, -17, -20, 17, -2, -13, 4, -4, 1, 20, -3, 15, 18, -19, -6, -5]

Why

`rotateArrayRight` returns `[17, -17, -20, 17, -2, -13, 4, -4, 1, 20, -3, 15, 18, -19, -6, -5]` 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.