Rotate an array to the right by positions, ideally in place.
Problem
Given an integer array nums, rotate the array to the right by k steps. Each step moves the last element of the array to the front.
You should modify the array in place when possible and return the rotated array conceptually through the updated input array.
Goal
After rotation, the element originally at index i should end up at index (i + k) mod n, where n is the array length.
Input Format
- An integer array
nums - An integer
krepresenting the number of right rotations
Output Format
- The same array after performing the rotation in place
Constraints
- Elements may be any integers
- Aim for time
- Extra space should be or as small as possible
Example 1
Input
nums = [1,2,3,4,5,6,7], k = 3
Output
[5,6,7,1,2,3,4]
Explanation
Rotating right by 3 moves the last three elements to the front.
Example 2
Input
nums = [-1,-100,3,99], k = 2
Output
[3,99,-1,-100]
Explanation
After two right rotations, the last two values appear at the beginning.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.