Here's a detailed, clean, and SEO-optimized blog post you can use for your website or blog page.
Rotating an array is a common problem asked in coding interviews and used in real-world applications such as data transformations and cyclic buffers. The most optimal approach to solve this is using the reverse algorithm, which works in O(n) time and O(1) space.

In this post, you'll learn:

  • ✅ What array rotation means
  • 🔁 How to rotate left and right by k positions
  • 🧠 Why the reverse algorithm is optimal
  • 💻 Clean Java code for both rotations

📌 What Is Array Rotation?

Left Rotation by k: Each element moves k positions to the left.

Example:

[1, 2, 3, 4, 5] rotated left by 2 → [3, 4, 5, 1, 2]

Right Rotation by k: Each element moves k positions to the right.

Example:

[1, 2, 3, 4, 5] rotated right by 2 → [4, 5, 1, 2, 3]


⚡ Optimal Approach: Reverse Algorithm

Instead of shifting elements one by one (which takes O(n×k) time), the reverse algorithm solves the problem in just 3 steps:

🔁 Left Rotate by k – Reverse Steps

  1. Reverse first k elements.
  2. Reverse remaining n - k elements.
  3. Reverse the whole array.

🔁 Right Rotate by k – Reverse Steps

  1. Reverse the whole array.
  2. Reverse first k elements.
  3. Reverse the remaining n - k elements.

💻 Java Code: Left Rotate Array by k Using Reverse Algorithm

import java.util.Arrays;

public class ArrayLeftRotate {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int k = 2;

        // Normalize k to avoid extra rotations
        k = k % arr.length;

        leftRotate(arr, k);

        System.out.println("Left Rotated Array: " + Arrays.toString(arr));
    }

    public static void leftRotate(int[] arr, int k) {
        int n = arr.length;

        reverse(arr, 0, k - 1);     // Step 1
        reverse(arr, k, n - 1);     // Step 2
        reverse(arr, 0, n - 1);     // Step 3
    }

    private static void reverse(int[] arr, int start, int end) {
        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }
}

✅ Output:

Left Rotated Array: [3, 4, 5, 1, 2]

💻 Java Code: Right Rotate Array by k Using Reverse Algorithm

import java.util.Arrays;

public class ArrayRightRotate {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int k = 2;

        // Normalize k to avoid extra rotations
        k = k % arr.length;

        rightRotate(arr, k);

        System.out.println("Right Rotated Array: " + Arrays.toString(arr));
    }

    public static void rightRotate(int[] arr, int k) {
        int n = arr.length;

        reverse(arr, 0, n - 1);     // Step 1
        reverse(arr, 0, k - 1);     // Step 2
        reverse(arr, k, n - 1);     // Step 3
    }

    private static void reverse(int[] arr, int start, int end) {
        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }
}

✅ Output:

Right Rotated Array: [4, 5, 1, 2, 3]

🧠 Why Reverse Algorithm is Best

Method Time Complexity Space Complexity Notes
Brute-force (shifting) O(n × k) O(1) Inefficient for large k
Using Temp Array O(n) O(k) Uses extra space
Reverse Algorithm O(n) O(1) ✅ Best choice

📘 Summary

  • Use reverse technique to rotate arrays in-place with optimal performance.
  • Normalize k using k % arr.length to avoid unnecessary rotations.
  • Practice both left and right versions to prepare for coding interviews.

🙋‍♂️ Interview Tip

💡 Always clarify whether the array is mutable or immutable and whether extra space is allowed. This will help you decide between the brute-force, temp-array, or reverse approach.