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.
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
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.
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 + " ");
}
}
}
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.