Left Rotate an Array by D Places

Easy
624.

Left Rotate an Array by D Places

Left Rotate an Array by D Places is an easy coding problem on arrays and two pointers, asked in TCS, Infosys, Wipro and Accenture interviews.

Rotate the array arr to the left by d positions and return the result. A left rotation by one moves arr[0] to the end.

d may be larger than the array length.

Example 1

Input
arr = [1,2,3,4,5,6,7], d = 2
Output
[3,4,5,6,7,1,2]
Explanation
The first two elements move to the back.

Example 2

Input
arr = [1,2,3], d = 4
Output
[2,3,1]
Explanation
Rotating by 4 is the same as rotating by 4 mod 3 = 1.

Example 3

Input
arr = [5,5,5], d = 0
Output
[5,5,5]

Constraints

  • 1 <= arr.length <= 100000
  • 0 <= d <= 1000000000
  • 1 <= arr[i] <= 1000000
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[]} arr
 * @param {number} d
 * @return {number[]}
 */
var rotateLeft = function(arr, d) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Left Rotate an Array by D Places — Problem & Solution

Left Rotate an Array by D Places solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Left Rotate an Array by D Places is an easy coding problem on arrays and two pointers, asked in TCS, Infosys, Wipro and Accenture interviews.

  • Difficulty: Easy
  • Topics: Arrays, Two Pointers
  • Asked at: TCS, Infosys, Wipro, Accenture
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Rotate the array arr to the left by d positions and return the result. A left rotation by one moves arr[0] to the end.

d may be larger than the array length.

Example 1

Input: arr = [1,2,3,4,5,6,7], d = 2
Output: [3,4,5,6,7,1,2]
Explanation: The first two elements move to the back.

Example 2

Input: arr = [1,2,3], d = 4
Output: [2,3,1]
Explanation: Rotating by 4 is the same as rotating by 4 mod 3 = 1.

Example 3

Input: arr = [5,5,5], d = 0
Output: [5,5,5]

Constraints

  • 1 <= arr.length <= 100000
  • 0 <= d <= 1000000000
  • 1 <= arr[i] <= 1000000

Hints for Left Rotate an Array by D Places

  1. Hint 1

    Rotating by n returns the array unchanged, so only d % n matters.

  2. Hint 2

    The answer is the suffix starting at d % n followed by the prefix before it.

  3. Hint 3

    The classic O(1)-space trick is the reversal algorithm: reverse the first d, reverse the rest, then reverse the whole thing.

How to solve Left Rotate an Array by D Places

A left rotation by d splits the array at index d % n and swaps the two blocks. Reducing d modulo n first is what makes a huge d free.

Approach

  1. Compute shift = d % n.
  2. The result is arr[shift..n-1] followed by arr[0..shift-1].
  3. For the in-place version: reverse arr[0..shift-1], reverse arr[shift..n-1], then reverse the whole array.

Why it works

Rotating by n is the identity, so the group of rotations is cyclic of order n and only the residue matters. The three-reversal trick works because reversing a block twice — once alone and once inside the full reversal — restores its order while moving it to the other side.

Complexity

  • Time — O(n)
  • Space — O(n) here; O(1) with the reversal algorithm

Pitfalls

  • Rotating one step at a time d times is O(n·d) and times out for large d.
  • Skipping the modulo indexes past the end as soon as d >= n.

Reference solution

Python

from typing import List

def rotateLeft(arr: List[int], d: int) -> List[int]:
    n = len(arr)
    shift = d % n
    return arr[shift:] + arr[:shift]

Java

public static int[] rotateLeft(int[] arr, int d) {
    int n = arr.length;
    int shift = d % n;
    int[] out = new int[n];
    for (int i = 0; i < n; i++) out[i] = arr[(i + shift) % n];
    return out;
}

C++

vector<int> rotateLeft(vector<int>& arr, int d) {
    int n = (int) arr.size();
    int shift = d % n;
    vector<int> out(n);
    for (int i = 0; i < n; i++) out[i] = arr[(i + shift) % n];
    return out;
}

JavaScript

var rotateLeft = function(arr, d) {
    var n = arr.length;
    var shift = d % n;
    var out = [];
    for (var i = 0; i < n; i++) out.push(arr[(i + shift) % n]);
    return out;
};

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