Write a Java Program for a given array of integers arr[] of size N and an integer, the task is to rotate the array elements to the left by d positions.
Examples:
Input:
arr[] = {1, 2, 3, 4, 5, 6, 7}, d = 2
Output: 3 4 5 6 7 1 2
Input: arr[] = {3, 4, 5, 6, 7, 1, 2}, d=2
Output: 5 6 7 1 2 3 4
Java Program For Array Rotation using temp array:
After rotating d positions to the left, the first d elements become the last d elements of the array
- First store the elements from index d to N-1 into the temp array.
- Then store the first d elements of the original array into the temp array.
- Copy back the elements of the temp array into the original array
Illustration:
Suppose the give array is arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2.
First Step:
=> Store the elements from 2nd index to the last.
=> temp[] = [3, 4, 5, 6, 7]
Second Step:
=> Now store the first 2 elements into the temp[] array.
=> temp[] = [3, 4, 5, 6, 7, 1, 2]
Third Steps:
=> Copy the elements of the temp[] array into the original array.
=> arr[] = temp[] So arr[] = [3, 4, 5, 6, 7, 1, 2]
Step-by-step approach:
- Initialize a temporary array(temp[n]) of length same as the original array
- Initialize an integer(k) to keep a track of the current index
- Store the elements from the position d to n-1 in the temporary array
- Now, store 0 to d-1 elements of the original array in the temporary array
- Lastly, copy back the temporary array to the original array
Below is the implementation of the above approach :
Java
import java.io.*;
class GFG {
static void Rotate( int arr[], int d, int n)
{
int temp[] = new int [n];
int k = 0 ;
for ( int i = d; i < n; i++) {
temp[k] = arr[i];
k++;
}
for ( int i = 0 ; i < d; i++) {
temp[k] = arr[i];
k++;
}
for ( int i = 0 ; i < n; i++) {
arr[i] = temp[i];
}
}
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 ;
Rotate(arr, d, N);
PrintTheArray(arr, N);
}
}
|
Time complexity: O(N)
Auxiliary Space: O(N)
Java Program For Array Rotation by rotating one-by-one:
- At each iteration, shift the elements by one position to the left circularly (i.e., first element becomes the last).
- Perform this operation d times to rotate the elements to the left by d position.
Illustration:
Let us take arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2.
First Step:
=> Rotate to left by one position.
=> arr[] = {2, 3, 4, 5, 6, 7, 1}
Second Step:
=> Rotate again to left by one position
=> arr[] = {3, 4, 5, 6, 7, 1, 2}
Rotation is done by 2 times.
So the array becomes arr[] = {3, 4, 5, 6, 7, 1, 2}
Step-by-step approach:
- Rotate the array to left by one position. For that do the following:
- Store the first element of the array in a temporary variable.
- Shift the rest of the elements in the original array by one place.
- Update the last index of the array with the temporary variable.
- Repeat the above steps for the number of left rotations required.
Below is the implementation of the above approach:
Java
import java.io.*;
class GFG {
public static void rotate( int arr[], int d, int n)
{
int p = 1 ;
while (p <= d) {
int last = arr[ 0 ];
for ( int i = 0 ; i < n - 1 ; i++) {
arr[i] = arr[i + 1 ];
}
arr[n - 1 ] = last;
p++;
}
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 ;
rotate(arr, d, N);
}
}
|
Time Complexity: O(N * d)
Auxiliary Space: O(1)
Java Program For Array Rotation using Juggling Algorithm:
Instead of moving one by one, divide the array into different sets where the number of sets is equal to the GCD of N and d (say X. So the elements which are X distance apart are part of a set) and rotate the elements within sets by 1 position to the left.
- Calculate the GCD between the length and the distance to be moved.
- The elements are only shifted within the sets.
- We start with temp = arr[0] and keep moving arr[I+d] to arr[I] and finally store temp at the right place.
Step-by-step approach:
- Perform d%n in order to keep the value of d within the range of the array where d is the number of times the array is rotated and N is the size of the array.
- Calculate the GCD(N, d) to divide the array into sets.
- Run a for loop from 0 to the value obtained from GCD.
- Store the value of arr[i] in a temporary variable (the value of i denotes the set number).
- Run a while loop to update the values according to the set.
- After exiting the while loop assign the value of arr[j] as the value of the temporary variable (the value of j denotes the last element of the ith set).
Below is the implementation of the above approach :
Java
import java.io.*;
class RotateArray {
void leftRotate( int arr[], int d, int n)
{
d = d % n;
int i, j, k, temp;
int g_c_d = gcd(d, n);
for (i = 0 ; i < g_c_d; i++) {
temp = arr[i];
j = i;
while ( true ) {
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break ;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
void printArray( int arr[], int size)
{
int i;
for (i = 0 ; i < size; i++)
System.out.print(arr[i] + " " );
}
int gcd( int a, int b)
{
if (b == 0 )
return a;
else
return gcd(b, a % b);
}
public static void main(String[] args)
{
RotateArray rotate = new RotateArray();
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
rotate.leftRotate(arr, 2 , 7 );
rotate.printArray(arr, 7 );
}
}
|
Time complexity : O(N)
Auxiliary Space : O(1)
Please refer complete article on Program for array rotation for more details!
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Nov, 2023
Like Article
Save Article