ABC Education's learner progress service stores ordered lesson identifiers in an array. Given an integer array nums and a non-negative integer k, rotate the array to the right by k positions and return the modified array.
Provide two solutions in this order:
- Brute force: move the last element to the front one position at a time.
- Optimal: use three in-place reversals. You must implement the reversal helper yourself and may not call a library reverse function or create a second array proportional to
nums.
Formal Specification
- Input:
nums, a mutable array of integers, and k, a non-negative integer.
- Output: The same array object after a right rotation by
k positions.
- Rotation should treat values in the original array as circular. Rotating by the array length, or any multiple of it, leaves the array unchanged.