208. Rotate Array

Medium
Array Reversal
Array

Problem

Given an integer array nums and a non-negative integer k, rotate the array to the right by k positions and return the resulting array.

Examples

Example 1

Input: [1,2,3,4,5,6,7], 3
Output: [5,6,7,1,2,3,4]

Example 2

Input: [-1,-100,3,99], 2
Output: [3,99,-1,-100]

Example 3

Input: [1,2], 0
Output: [1,2]
Constraints

1 <= nums.length <= 100000

0 <= k <= 1000000000

Hints

?? Reduce k using the array length.

?? Three reversals can perform the rotation in place.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

Can you do it in O(1) auxiliary space?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,2,3,4,5,6,7], 3
Expected
[5,6,7,1,2,3,4]