Left Rotate Array by K Places - Visualization - Visualization - Visualization

Visualization Player

Problem Statement

Given an array arr of integers and an integer k, your task is to rotate the array to the left by k positions.

In a left rotation, each element of the array is shifted to the left by one index, and the first elements are moved to the end in order.

The goal is to perform this rotation efficiently using an optimal in-place algorithm with O(n) time and O(1) extra space.

Examples

Input Array k Rotated Array Description
[10, 20, 30, 40, 50] 2 [30, 40, 50, 10, 20] Shift first 2 elements to the endVisualization
[10, 20, 30, 40] 0 [10, 20, 30, 40] No rotation since k = 0Visualization
[7, 8, 9] 3 [7, 8, 9] k equals array length, so array remains unchangedVisualization
[4, 5, 6] 5 [6, 4, 5] k > n, so we use k % n = 2 → rotate by 2 positionsVisualization
[1] 3 [1] Single element remains unchanged regardless of kVisualization
[] 4 [] Empty array remains unchangedVisualization

Solution

Understanding the Problem

We are given an array and asked to rotate it to the left by k positions. Rotating left means moving the first k elements to the end of the array, while keeping the rest of the elements in order.

For example, given arr = [1, 2, 3, 4, 5, 6, 7] and k = 3, rotating it to the left would give [4, 5, 6, 7, 1, 2, 3].

We will solve this problem step-by-step using an optimal method that is easy to understand and very efficient.

Step-by-Step Approach with Explanation

Step 1: Normalize k

If k is larger than the array size, rotating more than necessary is redundant. So, we first compute k = k % n, where n is the length of the array.

For example, rotating a 7-element array by 10 is the same as rotating it by 10 % 7 = 3 steps.

Step 2: Reverse the First k Elements

We reverse the first k elements. These are the ones that will eventually move to the end of the array after the full rotation.

In our example, reverse [1, 2, 3][3, 2, 1].

Step 3: Reverse the Remaining n - k Elements

Next, reverse the part of the array that will stay at the beginning after rotation — the last n - k elements.

In our example, reverse [4, 5, 6, 7][7, 6, 5, 4].

Step 4: Reverse the Entire Array

Now, reverse the whole array to finalize the rotation.

After combining the previous two steps: [3, 2, 1, 7, 6, 5, 4] → final reverse → [4, 5, 6, 7, 1, 2, 3].

Edge Cases and How We Handle Them

Case 1: k = 0

No rotation is needed. The array remains unchanged.

Case 2: k is a multiple of array length

If k % n == 0, the rotation brings us back to the original array. So we return it as-is.

Case 3: k > n

Normalize k using k % n to prevent over-rotating.

Case 4: Empty Array

If the array is empty, there's nothing to rotate. We simply return an empty array.

Case 5: Single-Element Array

Rotating a single-element array any number of times still results in the same array.

Algorithm Steps

  1. Given an array arr of size n and a value k.
  2. Normalize k using k = k % n to handle overflow.
  3. Reverse the first k elements.
  4. Reverse the remaining n - k elements.
  5. Reverse the entire array to get the final rotated form.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#include <stdio.h>
void reverse(int arr[], int start, int end) {
  while (start < end) {
    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    start++;
    end--;
  }
}

void leftRotate(int arr[], int n, int k) {
  k = k % n;
  reverse(arr, 0, k - 1);
  reverse(arr, k, n - 1);
  reverse(arr, 0, n - 1);
}

int main() {
  int arr[] = {1, 2, 3, 4, 5, 6, 7};
  int n = sizeof(arr) / sizeof(arr[0]);
  int k = 2;
  leftRotate(arr, n, k);
  printf("Rotated Array: ");
  for (int i = 0; i < n; i++) printf("%d ", arr[i]);
  return 0;
}

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(n)In all cases, the reversal algorithm performs three full reversals of parts of the array, resulting in linear time complexity.
Average CaseO(n)Regardless of the value of k, the algorithm always reverses the same number of elements in total, leading to consistent linear time.
Worst CaseO(n)Even in the worst case (e.g., k = n - 1), the three-reversal strategy still results in traversing each element once, so the complexity is linear.

Space Complexity

O(1)

Explanation: The algorithm operates in-place and uses only a constant number of temporary variables to perform reversals. No additional data structures are needed.

Detailed Step by Step Example

We will left rotate the array by 3 places using the reversal algorithm.

{ "array": [10,20,30,40,50,60,70], "showIndices": true }

Step 1: Reverse the first 3 elements.

{ "array": [10,20,30,40,50,60,70], "showIndices": true, "labels": { "0": "start", "2": "end" }, "highlightIndices": [0,1,2] }
{ "array": [30,20,10,40,50,60,70], "showIndices": true, "labels": { "0": "start", "2": "end" } }

Step 2: Reverse the remaining 4 elements.

{ "array": [30,20,10,40,50,60,70], "showIndices": true, "labels": { "3": "start", "6": "end" }, "highlightIndices": [3,4,5,6] }
{ "array": [30,20,10,70,60,50,40], "showIndices": true, "labels": { "3": "start", "6": "end" } }

Step 3: Reverse the entire array.

{ "array": [30,20,10,70,60,50,40], "showIndices": true, "labels": { "0": "start", "6": "end" }, "highlightIndices": [0,1,2,3,4,5,6] }
{ "array": [40,50,60,70,10,20,30], "showIndices": true, "labels": { "0": "start", "6": "end" } }

Final Rotated Array:

{ "array": [40,50,60,70,10,20,30], "showIndices": true, "highlightIndicesGreen": [4,5,6] }

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...