GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java Program to Left Rotate the Elements of an Array - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java Program to Left Rotate the Elements of an Array

Last Updated : 20 Aug, 2026

Left rotation of an array means shifting the elements to the left by a specified number of positions. The elements that are shifted out from the beginning are moved to the end of the array.

Example:

Input: arr[] = {1, 2, 3, 4, 5, 6, 7}, d = 2
Output: {3, 4, 5, 6, 7, 1, 2}

Input: arr[] = {10, 20, 30, 40, 50}, d = 3
Output: {40, 50, 10, 20, 30}

Different Approaches to Left Rotate an Array

There are several ways to left rotate an array in Java:

1. Using a Temporary Array

In this approach, we create a temporary array and first store the elements from index d to the end. Then, the first d elements are added to the end of the temporary array. Finally, the elements are copied back to the original array.

Java
class GFG {

    static void leftRotate(int[] arr, int d) {

        int n = arr.length;
        d = d % n;

        int[] temp = new int[n];
        int k = 0;

        // Store elements from d to the end
        for (int i = d; i < n; i++) {
            temp[k++] = arr[i];
        }

        // Store the first d elements
        for (int i = 0; i < d; i++) {
            temp[k++] = arr[i];
        }

        // Copy rotated elements back to original array
        for (int i = 0; i < n; i++) {
            arr[i] = temp[i];
        }
    }

    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 6, 7};

        leftRotate(arr, 2);

        for (int x : arr) {
            System.out.print(x + " ");
        }
    }
}

Output
3 4 5 6 7 1 2 

Explanation

  • d % n ensures that the rotation count stays within the array length.
  • Elements from index d to the end are copied first.
  • The first d elements are then added to the end.
  • The temporary array is copied back to the original array.

2. Rotating Elements One Position at a Time

In this approach, the array is rotated by one position repeatedly. The process is performed d times.

For each rotation:

  • Store the first element in a temporary variable.
  • Shift all remaining elements one position to the left.
  • Place the first element at the end
Java
class GFG {

    static void leftRotate(int[] arr, int d) {

        int n = arr.length;
        d = d % n;

        for (int j = 0; j < d; j++) {

            int first = arr[0];

            // Shift elements to the left
            for (int i = 0; i < n - 1; i++) {
                arr[i] = arr[i + 1];
            }

            // Move first element to the end
            arr[n - 1] = first;
        }
    }

    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 6, 7};

        leftRotate(arr, 2);

        for (int x : arr) {
            System.out.print(x + " ");
        }
    }
}

Output
3 4 5 6 7 1 2 

3. Using Juggling Algorithm

The Juggling Algorithm rotates an array efficiently by dividing its elements into sets based on the GCD (Greatest Common Divisor) of the array length and the number of rotations.

Approach

  • Reduce d using d % n.
  • Find gcd(n, d).
  • Divide the array into gcd(n, d) sets.
  • Move elements within each set by d positions.
  • Continue until all elements are rotated.
Java
class GFG {

    static int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    static void leftRotate(int[] arr, int d) {

        int n = arr.length;
        d = d % n;

        int g = gcd(n, d);

        for (int i = 0; i < g; i++) {

            int temp = arr[i];
            int j = i;

            while (true) {

                int k = j + d;

                if (k >= n) {
                    k -= n;
                }

                if (k == i) {
                    break;
                }

                arr[j] = arr[k];
                j = k;
            }

            arr[j] = temp;
        }
    }

    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 6, 7};

        leftRotate(arr, 2);

        for (int x : arr) {
            System.out.print(x + " ");
        }
    }
}
Try It Yourself
redirect icon

Output
3 4 5 6 7 1 2 

Explanation: The Juggling Algorithm moves elements directly to their rotated positions instead of repeatedly shifting the entire array.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.