ProGrad Companies Placement Prep Login Signup
Algorithms Data Structures Subjects Languages
PROCODER > KNOWLEDGEBASE > DATA STRUCTURES > ARRAY Go to testgym
Array Rotation | Program for Left and Right Rotation of an Array
05 min read
Array Rotation simply means shifting the array elements to the left or right of the array by speci ed
positions. An array can be rotated to the left(clockwise) or to the right (anti-clockwise) to the given number
of positions. Now let us look at a program for left rotation of an array and right rotation of an array.
Left Rotation of Array
Left rotation of an array means shifting of the elements in an array towards the left as shown in the below
image. Left rotation means rotating the elements of the array in a clockwise direction to the speci ed
number of positions.
C++ Program for Left Array Rotation
#include <bits/stdc++.h>
using namespace std;
void left_rotate_by_one(int arr[], int n)
{
/* Shift operation to the left */
int temp = arr[0], i;
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[i] = temp;
}
void array_left_rotate(int arr[], int no_of_rotations, int n)
{
for (int i = 0; i < no_of_rotations; i++)
left_rotate_by_one(arr, n); // Function is called for no_of_rotations times
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]); // Finding the size of the array
cout<<"\nArray elements before rotating : \n";
for(int i = 0; i < n; i++)
{
cout<<arr[i]<<"\t"; // Printing the array elements
}
int no_of_rotations = 1; // Number of rotations to take place
array_left_rotate(arr, no_of_rotations, n);
cout<<"\n\nArray elements after rotating : \n";
for(int i = 0; i < n; i++)
{
cout<<arr[i]<<"\t"; // Printing the array elements after rotation of elements
}
cout<<"\n";
return 0;
}
Java Program for Left Array Rotation
public class Main
{
/*Function to left rotate arr[] of size n by d*/
public static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n); // Function is called for no_of_rotation times
}
public static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1]; // Left shift by one
arr[i] = temp;
}
// Driver program to test above functions
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int no_of_rotations = 1;
int n = arr.length;
System.out.println("Array Elements before rotating : ");
for(int i = 0 ; i < n ; i++)
{
System.out.print(arr[i]+ " "); // Printing elements before rotation
}
leftRotate(arr, no_of_rotations, n);
System.out.println("\nArray Elements after rotating : ");
for(int i = 0 ; i < n ; i++)
{
System.out.print(arr[i] + " "); // Printing elements after rotation
}
}
}
Python Program for Left Array Rotation
def left_rotate_by_one(arr, n):
temp = arr[0];
for i in range (n - 1):
arr[i] = arr[i + 1]; // Left shift by one
arr[n - 1] = temp;
def leftRotate(arr, d, n):
for i in range(0, d):
left_rotate_by_one(arr, n) // Function is called for no_of_rotation times
# Main Function
import array as arr
arr = arr.array('i', [1,2,3,4,5,6,7])
n = len(arr)
no_of_rotations = 1
print("Array Elements before rotation : ") # printing array before rotating
for i in range (0, n):
print (arr[i], end = ' ')
leftRotate(arr, no_of_rotations, n )
print("\nArray Elements after rotation : ") # printing array after rotating
for i in range (0, n):
print (arr[i], end = ' ')
OUTPUT
Right Rotation of Array
Right rotation of an array means rotating the array elements towards the right of the array as shown in the
below image. Right rotation means rotating the elements of the array in the anti-clockwise direction to the
speci ed number of positions.
C++ Program for Right Array Rotation
#include <bits/stdc++.h>
using namespace std;
void right_rotate_by_one(int arr[], int n)
{
/* Shift operation to the right */
int temp = arr[n - 1], i;
for (i = n - 1; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = temp;
}
void array_right_rotate(int arr[], int no_of_rotations, int n)
{
for (int i = 0; i < no_of_rotations; i++)
right_rotate_by_one(arr, n); // Function is called for no_of_rotations times
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]); // Finding the size of the array
cout<<"\nArray elements before rotating : \n";
for(int i = 0; i < n; i++)
{
cout<<arr[i]<<"\t"; // Printing the array elements
}
int no_of_rotations = 2;
array_right_rotate(arr, no_of_rotations, n);
cout<<"\n\nArray elements after rotating : \n";
for(int i = 0; i < n; i++)
{
cout<<arr[i]<<"\t"; // Printing the array elements after rotation of elements
}
cout<<"\n";
return 0;
}
Java Program for Right Array Rotation
public class Main
{
/*Function to left rotate arr[] of size n by d*/
public static void rightRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
rightRotatebyOne(arr, n); // Function is called for no_of_rotation times
}
public static void rightRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[n - 1];
for (i = n-1; i > 0; i--)
arr[i] = arr[i - 1]; // Right shift by one
arr[0] = temp;
}
// Driver program to test above functions
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int no_of_rotations = 2;
int n = arr.length;
System.out.println("Array Elements before rotating : ");
for(int i = 0 ; i < n ; i++)
{
System.out.print(arr[i]+ " "); // Printing elements before rotation
}
rightRotate(arr, no_of_rotations, n);
System.out.println("\nArray Elements after rotating : ");
for(int i = 0 ; i < n ; i++)
{
System.out.print(arr[i] + " "); // Printing elements after rotation
}
}
}
Python Program for Right Array Rotation
def right_rotate_by_one(arr, n):
temp = arr[n-1];
for i in range (n - 1 , 0, -1):
arr[i] = arr[i - 1]; // Right shift by one
arr[0] = temp;
def rightRotate(arr, d, n):
for i in range(0, d):
right_rotate_by_one(arr, n) // Function is called for no_of_rotation times
# Main Function
import array as arr
arr = arr.array('i', [1,2,3,4,5,6,7])
# printing original array
print ("The new created array is : ")
n = len(arr)
no_of_rotations = 2
print("Array Elements before rotation : ")
for i in range (0, n):
print (arr[i], end = ' ')
rightRotate(arr, no_of_rotations, n )
print("\nArray Elements after rotation : ")
for i in range (0, n):
print (arr[i], end = ' ')
OUTPUT
Relevant exercises
Questions on Array Questions on Linked List Questions on Graph
10 Questions 10 Questions 10 Questions
ARRAY LINKED LIST GRAPH
Questions on Queue Questions on B and B+ Trees
Show More
10 Questions 10 Questions
QUEUE BINARY TREE
Previous Article Next Article
Introduction to Arrays | Types of Advantages and Disadvantages
Arrays and their Representation of Arrays in C, C++ and Java
Array implementation and its types are Advantages and disadvantages of arrays
discussed here. are discussed in this article
08 min read 10 min read
POST A NEW COMMENT
Post comment
PROcoder KnowledgeBase TestGym
KnowledgeBase Algorithms Algorithms
TestGym Data Structures Data Structures
Subjects Subjects
Languages Languages