DEV Community

Dev Cookies
Dev Cookies

Posted on

πŸ”„ Rotate Array in Java (Left & Right) Using Reverse Algorithm β€” Clean Code Explained

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--;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Left Rotated Array: [3, 4, 5, 1, 2]
Enter fullscreen mode Exit fullscreen mode

πŸ’» 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--;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Right Rotated Array: [4, 5, 1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

🧠 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.

Top comments (0)