Array Rotation In Java
Rotation of an array at the given Index
Checkout this Array Rotation in Java page you’ll get the solution to a problem where the elements of an array need to be rotated based on their index positions.
For instance, given an array Arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}, rotating it to the left by 2 positions results in Arr = {3, 4, 5, 6, 7, 8, 9, 1, 2}. Similarly, rotating the same array to the right by 2 positions gives Arr = {8, 9, 1, 2, 3, 4, 5, 6, 7}.
Array Rotation in Java
Rotation of Array is a fundamental concept in data structures where the elements of an array are shifted left or right by a given number of positions.
This is often asked in coding interviews to evaluate understanding of array manipulation and optimization techniques.
Problem Statement
Given an array of integers and a number k, rotate the array:
- Left Rotation: Move each element of the array to the left by k positions.
- Right Rotation: Move each element of the array to the right by k positions.
Example:
Input: arr = [1, 2, 3, 4, 5], k = 2
Left Rotation Output: [3, 4, 5, 1, 2] Right Rotation Output: [4, 5, 1, 2, 3]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Types of Array Rotation in Java Programming
- Left Rotation
- Right Rotation
Each can be implemented using:
- Naive method (with loops)
- Using temporary arrays
- Using Juggling algorithm (advanced)
- Using reversal algorithm
Methods of Array Rotation (Left Rotation) using Java
Method 1: Naive (One by One Rotation)
Algorithm:
Repeat d times:
- Store the first element.
- Shift all elements left by 1.
- Place the stored element at the end.
Space Complexity: O(1)
Code:
public class NaiveRotation { public static void leftRotate(int[] arr, int d) { int n = arr.length; for (int i = 0; i < d; i++) { int temp = arr[0]; for (int j = 0; j < n - 1; j++) { arr[j] = arr[j + 1]; } arr[n - 1] = temp; } } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; leftRotate(arr, 2); System.out.println(java.util.Arrays.toString(arr)); // Output: [3, 4, 5, 1, 2] } }
Method 2: Using Temporary Array
Algorithm:
Create a temporary array of size d and copy first d elements.
Shift the remaining elements to the left.
Copy the temporary elements to the end.
Space Complexity: O(d)
Code:
public class TempArrayRotation { public static void leftRotate(int[] arr, int d) { int n = arr.length; int[] temp = new int[d]; // Step 1: Store first d elements for (int i = 0; i < d; i++) temp[i] = arr[i]; // Step 2: Shift remaining n - d elements for (int i = d; i < n; i++) arr[i - d] = arr[i]; // Step 3: Copy back the d elements for (int i = 0; i < d; i++) arr[n - d + i] = temp[i]; } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; leftRotate(arr, 2); System.out.println(java.util.Arrays.toString(arr)); // Output: [3, 4, 5, 1, 2] } }
Methods of Array Rotation using Java
Method 3: Juggling Algorithm
Algorithm:
Divide the array into GCD(d, n) sets.
In each set, cyclically rotate elements.
Space Complexity: O(1)
Code:
public class JugglingRotation { public static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } public static void leftRotate(int[] arr, int d) { int n = arr.length; d = d % n; // handle d > n int g = gcd(d, n); for (int i = 0; i < g; i++) { int temp = arr[i]; int j = i; while (true) { int k = j + d; if (k >= n) k = 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}; leftRotate(arr, 2); System.out.println(java.util.Arrays.toString(arr)); // Output: [3, 4, 5, 1, 2] } }
Method 4: Reversal Algorithm
Algorithm:
Reverse first d elements.
Reverse last n-d elements.
Reverse the entire array.
Space Complexity: O(1)
Code:
public class ReversalRotation { public static void reverse(int[] arr, int start, int end) { while (start < end) { int temp = arr[start]; arr[start++] = arr[end]; arr[end--] = temp; } } public static void leftRotate(int[] arr, int d) { int n = arr.length; d = d % n; reverse(arr, 0, d - 1); reverse(arr, d, n - 1); reverse(arr, 0, n - 1); } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; leftRotate(arr, 2); System.out.println(java.util.Arrays.toString(arr)); // Output: [3, 4, 5, 1, 2] } }
In left rotation, you move elements toward the start of the array.
In right rotation, you move elements toward the end of the array.
So, for each method, you adjust the direction and indexing to shift elements right instead of left.
Conclusion:
Array rotation is a fundamental operation often used in search algorithms, circular queues, and buffer operations. Depending on the constraints:
- Use Reversal or Juggling algorithms for space-efficient, fast solutions.
- Use temporary arrays for simplicity when extra space is not a constraint.
- Avoid naive approach for large n and d due to poor performance.
FAQ's related to Rotation of Array
Answer:
Array rotation is the process of shifting all elements of an array to the left or right by a given number of positions (d).
- In left rotation, elements move toward the beginning, and the first d elements are moved to the end.
- In right rotation, elements move toward the end, and the last d elements are moved to the beginning.
Answer:
There are four widely used techniques to perform array rotation:
- Naive Method (rotate one element at a time)
- Using Temporary Array
- Juggling Algorithm (based on GCD)
- Reversal Algorithm (most efficient and in-place)
Answer:
Always normalize the rotation count using:
d = d % n;
Answer:
Not always. Only Naive and Temporary Array methods preserve the original relative order of elements with equal values (i.e., are stable).
Juggling and Reversal methods are not stable.
Answer:
Yes, with adaptation. You can either:
- Convert right rotation to left by doing leftRotate(arr, n – d), or
- Adjust the logic in each left rotation algorithm (e.g., reversing, index shifting) to rotate right directly.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
package com.company1;
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static int[] rotation(int[] arr,int k)
{
int j = 0;
while(k>0)
{
int temp = arr[arr.length-1];
for(j=arr.length-1;j>0;j–)
{
arr[j] = arr[j-1];
}
arr[j] = temp;
k–;
}
return arr;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println(“how many numbers you want to put in arrays ?”);
int number = sc.nextInt();
System.out.println(“now print those ” + number + ” arrays :”);
int[] originalArray = new int[number];
for(int i=0;i<originalArray.length;i++)
{
int values = sc.nextInt();
originalArray[i] = values;
}
int[] duplicateArray = new int[originalArray.length];
duplicateArray = Arrays.copyOf(originalArray,originalArray.length);
System.out.println("now everything is set ,just tell me to how many places you have to right rotate your arrays?");
int k = sc.nextInt();
int[] printArray = rotation(duplicateArray,k);
System.out.println("Here is your original array :");
for(int i=0;i<originalArray.length;i++)
{
System.out.print(" " + originalArray[i]);
}
System.out.println();
System.out.println("Here is your operated array");
System.out.println();
for(int i=0;i<printArray.length;i++)
{
System.out.print(" " + printArray[i]);
}
}
}