Rotate an Array Right (Set 3)

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

Example 1

rotateArrayRight()
Input
nums = [-6, 12, 8, 20, 14, -1, -9, -5, 9, 9, -16, 10, 16, 0, 14, -20], k = 3
Output
[0, 14, -20, -6, 12, 8, 20, 14, -1, -9, -5, 9, 9, -16, 10, 16]

Why

`rotateArrayRight` returns `[0, 14, -20, -6, 12, 8, 20, 14, -1, -9, -5, 9, 9, -16, 10, 16]` for this input.

Example 2

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

Why

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