Java Program for Reversal algorithm for array rotation Last Updated : 10 Oct, 2023 Comments Improve Suggest changes Like Article Like Report Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. Example : Input : arr[] = [1, 2, 3, 4, 5, 6, 7] d = 2Output : arr[] = [3, 4, 5, 6, 7, 1, 2] Rotation of the above array by 2 will make array Algorithm : rotate(arr[], d, n) reverse(arr[], 1, d) ; reverse(arr[], d + 1, n); reverse(arr[], 1, n);Let AB are the two parts of the input array where A = arr[0..d-1] and B = arr[d..n-1]. The idea of the algorithm is : Reverse A to get ArB, where Ar is reverse of A.Reverse B to get ArBr, where Br is reverse of B.Reverse all to get (ArBr) r = BA.Example : Let the array be arr[] = [1, 2, 3, 4, 5, 6, 7], d =2 and n = 7A = [1, 2] and B = [3, 4, 5, 6, 7] Reverse A, we get ArB = [2, 1, 3, 4, 5, 6, 7]Reverse B, we get ArBr = [2, 1, 7, 6, 5, 4, 3]Reverse all, we get (ArBr)r = [3, 4, 5, 6, 7, 1, 2]Below is the implementation of the above approach: Java // Java program for reversal algorithm of array rotation import java.io.*; class LeftRotate { /* Function to left rotate arr[] of size n by d */ static void leftRotate(int arr[], int d) { if (d == 0) return; int n = arr.length; // in case the rotating factor is // greater than array length d = d % n; reverseArray(arr, 0, d - 1); reverseArray(arr, d, n - 1); reverseArray(arr, 0, n - 1); } /*Function to reverse arr[] from index start to end*/ static void reverseArray(int arr[], int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } /*UTILITY FUNCTIONS*/ /* function to print an array */ static void printArray(int arr[]) { for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " "); } /* Driver program to test above functions */ public static void main(String[] args) { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int n = arr.length; int d = 2; leftRotate(arr, d); // Rotate array by d printArray(arr); } } /*This code is contributed by Devesh Agrawal*/ Output3 4 5 6 7 1 2 Time Complexity: O(N)Auxiliary Space: O(1) Please refer complete article on Reversal algorithm for array rotation for more details! Comment More infoAdvertise with us Next Article Java Program for Reversal algorithm for array rotation kartik Follow Improve Article Tags : Java Programs DSA Arrays rotation Practice Tags : Arrays Similar Reads Java Program for Reversal algorithm for right rotation of an array Given an array, right rotate it by k elements. After K=3 rotation Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} k = 3 Output: 8 9 10 1 2 3 4 5 6 7 Input: arr[] = {121, 232, 33, 43 ,5} k = 2 Output: 43 5 121 232 33 Note : In the below solution, k is assumed to be smaller than or equal to n 2 min read Java Program for Block swap algorithm for array rotation Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. Rotation of the above array by 2 will make array Algorithm : Initialize A = arr[0..d-1] and B = arr[d..n-1] 1) Do following until size of A is equal to size of B a) If A is shorter, divide B into Bl and Br such that Br i 4 min read Program to reverse the rows in a 2d Array Given a 2D array arr[][] of size M x N integers where M is the number of rows and N is the number of columns. The task is to reverse every row of the given 2D array.Example: Input: arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }Output:3 2 16 5 49 8 7Input: arr[][] = { {1, 2}, {4, 5}, {7, 8}, {9, 10} } 9 min read Java Program to Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: So 4 min read Java Program to Print array after it is right rotated K times Given an Array of size N and a values K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples :  Input: Array[] = {1, 3, 5, 7, 9}, K = 2. Output: 7 9 1 3 5 Explanation: After 1st rotation - {9, 1, 3, 5, 7} After 2nd rotation - {7, 9, 1, 3, 5} Input: 2 min read Java Program for Left Rotation and Right Rotation of a String Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d <= n)Right (Or clockwise) rotate the given string by d elements (where d <= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : 6 min read Java Program to Reverse a List Reversing a list means after reversing the list, the first element should be swapped with the last element, the second element should be swapped with the second last element, and so on till the middle element and the resultant list is the reversed list. Example: Input: "PLATFORM", "LEARNING", "BEST" 3 min read Java Program for Rotate a Matrix by 180 degree Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input : 1 2 3 4 5 6 7 8 9 Output : 9 8 7 6 5 4 3 2 1 Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 Method: 1 (Only prints ro 6 min read Java Program to Traverse Through ArrayList in Reverse Direction ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. The task is to insert an 4 min read How to Left or Right rotate an Array in Java? Given an array arr[] of size N and D index, the task is to rotate the array by the D index. We have two flexibilities either to rotate them leftwards or rightwards via different ways which we are going to explore by implementing every way of rotating in both of the rotations. Ways: Using temporary a 15+ min read Like