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 specified 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 specified number of positions.

Left array rotation

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


program for left array rotation with 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 specified number of positions.

Right array rotation

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

program for right array rotation with output

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error