Java Program to Left Rotate the Elements of an Array
In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.
Example:
We can use a temporary array to rotate the array left by "d" positions. This approach is useful when the array size is not too large. Also, the temporary array does not impact the memory constraints.
// Java program to rotate an array
// to the left by "d" positions
import java.io.*;
class GFG {
// Function to rotate array
static void Rotate(int arr[], int d, int n) {
// Storing rotated version of array
int temp[] = new int[n];
// Keeping track of the current index of temp[]
int k = 0;
// Storing the n - d elements of array arr[] to the front of temp[]
for (int i = d; i < n; i++) {
temp[k] = arr[i];
k++;
}
// Storing the first d elements of array arr[] into temp
for (int i = 0; i < d; i++) {
temp[k] = arr[i];
k++;
}
// Copying the elements of temp[] into arr[] to get the final rotated array
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
// Function to print elements of the array
static void PrintTheArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int N = arr.length;
int d = 2;
// Function calling
Rotate(arr, d, N);
PrintTheArray(arr, N);
}
}
Output
3 4 5 6 7 1 2
Other Ways to Left Rotate the Elements of an Array
Rotating Elements One-by-One
In this approach, we rotate the array to left by one position at each step. After performing this operation "d"
times, the array is rotated to the left by "d"
positions. This method is simple and effective for small values of "d"
.
Example:
// Java program to left rotate
// an array by "d" positions
import java.io.*;
class GFG {
// Function to rotate the array to the left by d positions
public static void rotate(int arr[], int d, int n) {
int p = 1;
while (p <= d) {
// Store the first element in a temporary variable
int first = arr[0];
// Shift each element one position to the left
for (int i = 0; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
// Place the first element at the end of the array
arr[n - 1] = first;
p++;
}
// Print the rotated array
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int N = arr.length;
// Rotate the array 2 times to the left
int d = 2;
// Function call to rotate the array
rotate(arr, d, N);
}
}
Output
3 4 5 6 7 1 2
Using Juggling Algorithm
In this approach, it optimizes the rotation by using the GCD of "N" and "d" to divide the arrays into sets. Here, each set will rotate internally. It is an efficient approach for large arrays because it reduces the number of element moves.
Steps:
- Calculate
d % N
to ensure rotations stay within array bounds. - Compute the GCD of
N
andd
to determine the number of sets. - For each set, store the set's first element in a temporary variable.
- Rotate elements within the set by moving each element
d
positions left. - Assign the temporary variable to the last position in the set.
Example:
// Java program to rotate an array
// to the left by "d" elements
import java.io.*;
class RotateArray {
// Function to left rotate arr[] of size n by d positions
void leftRotate(int arr[], int d, int n) {
// If d >= n, reduce d to a value within the array bounds
d = d % n;
int i, j, k, temp;
int g_c_d = gcd(d, n); // Calculate GCD of d and n
// Process each set of elements as per GCD value
for (i = 0; i < g_c_d; i++) {
// Move i-th element of each block
temp = arr[i];
j = i;
while (true) {
k = j + d; // Calculate index to swap with
// Wrap around if k exceeds array length
if (k >= n)
k = k - n;
// Break when we complete the cycle for this block
if (k == i)
break;
arr[j] = arr[k]; // Move elements within the set
j = k;
}
arr[j] = temp; // Place initial element at end of cycle
}
}
// Utility function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");
}
// Function to calculate GCD of a and b
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// Driver program to test the left rotation function
public static void main(String[] args) {
RotateArray rotate = new RotateArray();
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int n = arr.length;
int d = 2; // Number of positions to rotate
rotate.leftRotate(arr, d, n);
rotate.printArray(arr, n);
}
}
Output
3 4 5 6 7 1 2