Write a Java program to rotate an array to the left or right by n steps is a frequently asked Java interview question.
For example if your array is– {1,2,3,4,5,6,7,8} then rotating elements of array by 2 steps to the right will make the array as {7,8,1,2,3,4,5,6} where as rotating to the left by 2 positions will give the output as {3,4,5,6,7,8,1,2}
Array rotation program- Solution
In this post two solutions are given for array rotation program-
- Using temporary array and array copying. See example.
- Rotating one element at a time using loops. See example.
Array rotation program- Using temp array
Solution using temporary array works as follows-
- If you have to rotate by 2 steps i.e. n=2 then copy n elements to a temporary array.
- Shift rest of the elements to the left or right based on rotation requirement.
- Copy elements from the temp array to the original array in the space created by shifting the elements.
In the program we actually copy all the elements to the temp array and then copy back to original array.
public class ArrayRotation { public static void main(String[] args) { int[] numArr={1,2,3,4,5,6,7,8}; //rotateLeft(numArr, 4); rotateRight(numArr, 3); } private static void rotateLeft(int[] numArr, int steps){ // create temp array int[] temp = new int[numArr.length]; // copy elements to the temp array for(int i = 0; i < steps; i++){ temp[(numArr.length-steps)+ i] = numArr[i]; } // copy rest of the elements from the original array int i = 0; for(int j = steps; j < numArr.length; j++, i++){ temp[i] = numArr[j]; } //copy from temp to original System.arraycopy(temp, 0, numArr, 0, numArr.length); System.out.println("Array after left rotation- " + Arrays.toString(numArr)); } private static void rotateRight(int[] numArr, int steps){ // create temp array int[] temp = new int[numArr.length]; // copy elements to the temp array for(int i = 0; i < steps; i++){ temp[i] = numArr[(numArr.length-steps)+ i]; } // copy rest of the elements from the original array int i = steps; for(int j = 0; j < numArr.length - steps; j++, i++){ temp[i] = numArr[j]; } System.out.println("Array after right rotation- " + Arrays.toString(temp)); } }
Output
Array after right rotation- [6, 7, 8, 1, 2, 3, 4, 5]
Array rotation program- using loops
This Java program for array rotation uses inner and outer for loops for shifting and copying elements.
Solution using loops works as follows-- Copy the first element (in case of left rotation) or last element (in case of right rotation) in a temporary variable.
- Shift elements to the left or right as per rotation requirement in an inner loop one step at a time.
- Once out of inner loop copy the element stored in temp variable to its final position.
public class ArrayRotation { public static void main(String[] args) { int[] numArr={1,2,3,4,5,6,7,8}; rotateLeft(numArr, 2); //rotateRight(numArr, 3); } private static void rotateLeft(int[] numArr, int steps){ for(int i = 0; i < steps; i++){ // store the first element int temp = numArr[0]; for(int j = 0; j < numArr.length - 1; j++){ // shift element to the left by 1 position numArr[j] = numArr[j + 1]; } // copy stored element to the last numArr[numArr.length - 1] = temp; } System.out.println("Array after left rotation- " + Arrays.toString(numArr)); } private static void rotateRight(int[] numArr, int steps){ for(int i = 0; i < steps; i++){ int temp = numArr[numArr.length-1]; for(int j = numArr.length-1; j > 0; j--){ numArr[j] = numArr[j -1]; } // copy stored element to the beginning numArr[0] = temp; } System.out.println("Array after right rotation- " + Arrays.toString(numArr)); } }
Output
Array after left rotation- [3, 4, 5, 6, 7, 8, 1, 2]
That's all for this topic Array Rotation Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Programs Page
Related Topics
You may also like-