Rotate Array

Medium
15.

Rotate Array

Rotate Array is a medium coding problem on arrays, math and two pointers, asked in Mindtree, Razorpay and Virtusa interviews.

Given an integer array nums, rotate the array to the right by k steps (k is non-negative) and return the result.

Example 1

Input
nums = [1,2,3,4,5,6,7], k = 3
Output
[5,6,7,1,2,3,4]
Explanation
Rotate right 3 times: [7,1,…], [6,7,1,…], [5,6,7,1,2,3,4].

Example 2

Input
nums = [-1,-100,3,99], k = 2
Output
[3,99,-1,-100]

Constraints

  • 1 <= nums.length <= 30
  • -100 <= nums[i] <= 100
  • 0 <= k <= 100

Follow-up: the classic in-place trick reverses the whole array, then each half.

Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var rotate = function(nums, k) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Rotate Array — Solution in Python, Java & C++

Rotate Array solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem on arrays and math.

Rotate Array is a medium coding problem on arrays, math and two pointers, asked in Mindtree, Razorpay and Virtusa interviews.

Problem statement

Given an integer array nums, rotate the array to the right by k steps (k is non-negative) and return the result.

Example 1

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation: Rotate right 3 times: [7,1,…], [6,7,1,…], [5,6,7,1,2,3,4].

Example 2

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

Constraints

  • 1 <= nums.length <= 30
  • -100 <= nums[i] <= 100
  • 0 <= k <= 100

Follow-up: the classic in-place trick reverses the whole array, then each half.

Hints for Rotate Array

  1. Hint 1

    k can exceed the length — reduce it with k % n first.

  2. Hint 2

    The last k % n elements move to the front, everything else shifts right.

How to solve Rotate Array

Rotating right by k moves the element at index i to (i + k) % n. The elegant in-place recipe is three reversals: reverse the whole array, then reverse the first k elements, then reverse the rest. That lands every element in the right place with no extra array.

Approach

  1. Reduce k modulo n — rotating by n is a no-op, and k may exceed the length.
  2. Reverse the entire array.
  3. Reverse the first k elements.
  4. Reverse the remaining n - k elements.

Why it works

Reversing the whole array brings the last k elements to the front but in the wrong order, and leaves the first n - k at the back, also reversed. Reversing each of those two blocks restores their internal order, which is exactly the rotation. Since each reversal is its own inverse on its block, the composition is a pure cyclic shift — and it touches each element a constant number of times.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Take k % n first, or the block boundaries fall outside the array.
  • k may be 0 after the modulo, in which case the three reversals correctly cancel out.
  • The direction matters: this rotates right; rotating left needs n - k.
  • Reverse the whole array first — reversing the halves first gives a different (wrong) permutation.

Reference solution

Python

from typing import List

def rotate(nums: List[int], k: int) -> List[int]:
    n = len(nums)
    s = k % n
    return nums[n - s:] + nums[:n - s]

Java

public static int[] rotate(int[] nums, int k) {
    int n = nums.length;
    int shift = k % n;
    reverseRange(nums, 0, n - 1);
    reverseRange(nums, 0, shift - 1);
    reverseRange(nums, shift, n - 1);
    return nums;
}

private static void reverseRange(int[] nums, int lo, int hi) {
    while (lo < hi) {
        int t = nums[lo];
        nums[lo] = nums[hi];
        nums[hi] = t;
        lo++;
        hi--;
    }
}

C++

void reverseRange(vector<int>& nums, int lo, int hi) {
    while (lo < hi) {
        int t = nums[lo];
        nums[lo] = nums[hi];
        nums[hi] = t;
        lo++;
        hi--;
    }
}

vector<int> rotate(vector<int>& nums, int k) {
    int n = (int) nums.size();
    int shift = k % n;
    reverseRange(nums, 0, n - 1);
    reverseRange(nums, 0, shift - 1);
    reverseRange(nums, shift, n - 1);
    return nums;
}

JavaScript

var rotate = function(nums, k) {
    const n = nums.length;
    const s = k % n;
    return nums.slice(n - s).concat(nums.slice(0, n - s));
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Two Pointers Technique