Array Rotation in C++

Rotation of array in C++

On this page we will discuss about rotation of array in C++ . Array rotation is a process of rearranging the elements of an array by shifting its elements by a specified number of positions.This can be done in two ways: left rotation and right rotation.

 
Array-Rotation-in-Cpp Programming

Array Rotation In C++ Programming

Array rotation is a common operation in computer programming that involves shifting the elements of an array by a certain number of positions. This can be done in two ways: left rotation and right rotation.

  • In left rotation, the elements of the array are shifted to the left by a certain number of positions, and the elements that “fall off” the left end of the array are added to the right end of the array. For example, if we have an array [1, 2, 3, 4, 5] and we left rotate it by 2 positions, we would get the array [3, 4, 5, 1, 2].
  • In right rotation, the elements of the array are shifted to the right by a certain number of positions, and the elements that “fall off” the right end of the array are added to the left end of the array. For example, if we have an array [1, 2, 3, 4, 5] and we right rotate it by 2 positions, we would get the array [4, 5, 1, 2, 3].
Rotation of array In C++

Algorithm for ( Using a Temporary Array )

  1. Create a temporary array of size d.
  2. Copy the first d elements of the original array to the temporary array.
  3. Shift the remaining elements of the original array to the left by d positions.
  4. Add the elements from the temporary array to the right end of the original array.
  5. The array is now left rotated by d positions.

C++ code for temporary array method

Run
#include <iostream>
using namespace std;

void leftRotate (int arr[], int n, int d)
{
  int temp[d];
  for (int i = 0; i < d; i++)
    {
      temp[i] = arr[i];
    }
  for (int i = d; i < n; i++)
    {
      arr[i - d] = arr[i];
    }
  for (int i = n - d, j = 0; i < n; i++, j++)
    {
      arr[i] = temp[j];
    }
}

void rightRotate (int arr[], int n, int d)
{
  int temp[d];
  for (int i = n - d, j = 0; i < n; i++, j++)
    {
      temp[j] = arr[i];
    }
  for (int i = n - d - 1; i >= 0; i--)
    {
      arr[i + d] = arr[i];
    }
  for (int i = 0; i < d; i++)
    {
      arr[i] = temp[i];
    }
}

int main ()
{
  int arr[] = { 1, 2, 3, 4, 5 };
  int n = sizeof (arr) / sizeof (arr[0]);
  int d = 2;

  cout << "Original Array: ";
  for (int i = 0; i < n; i++)
    {
      cout << arr[i] << " ";
    }
  cout << endl;

  leftRotate (arr, n, d);
  cout << "Left Rotated Array: ";
  for (int i = 0; i < n; i++)
    {
      cout << arr[i] << " ";
    }
  cout << endl;

  rightRotate (arr, n, d);
  cout << "Right Rotated Array: ";
  for (int i = 0; i < n; i++)
    {
      cout << arr[i] << " ";
    }
  cout << endl;

  return 0;
}

Output

Original Array: 1 2 3 4 5 
Left Rotated Array: 3 4 5 1 2 
Right Rotated Array: 1 2 3 4 5 

Algorithm for ( Rotating One by One )

  1. For each rotation, store the first element of the array in a temporary variable.
  2. Shift all the remaining elements of the array one position to the left.
  3. Move the temporary variable to the end of the array.
  4. Repeat the above steps d times to left rotate the array by d positions.

C++ code for rotating one by one

Run
#include <iostream>
using namespace std;

void leftRotate (int arr[], int n, int d)
{
  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;
    }
}

void rightRotate (int arr[], int n, int d)
{
  for (int i = 0; i < d; i++)
    {
      int temp = arr[n - 1];
      for (int j = n - 1; j >= 1; j--)
	{
	  arr[j] = arr[j - 1];
	}
      arr[0] = temp;
    }
}


int main ()
{
  int arr[] = { 1, 2, 3, 4, 5 };
  int n = sizeof (arr) / sizeof (arr[0]);
  int d = 2;

  cout << "Original Array: ";
  for (int i = 0; i < n; i++)
    {
      cout << arr[i] << " ";
    }
  cout << endl;

  leftRotate (arr, n, d);
  cout << "Left Rotated Array: ";
  for (int i = 0; i < n; i++)
    {
      cout << arr[i] << " ";
    }
  cout << endl;

  rightRotate (arr, n, d);
  cout << "Right Rotated Array: ";
  for (int i = 0; i < n; i++)
    {
      cout << arr[i] << " ";
    }
  cout << endl;

  return 0;
}

Output

Original Array: 1 2 3 4 5 
Left Rotated Array: 3 4 5 1 2 
Right Rotated Array: 1 2 3 4 5 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java

Array Rotation in C

Array rotation in C

Array rotation

In this Article we’ll be learning about program for array rotation of elements of array – left and right to a specified number of times. An array is said to be right rotated if all the selected elements were  moved towards right by one position.

Array Rotation In C

Method Discussed :

  • Method 1 : Using Temporary array.
  • Method 2 : Rotate one by one.

Method 1 :

In this method we will declare an extra array to store some k elements. Here, k refers to number of rotations.

  • Declare a temporary array of size k.
  • Store the first k elements in temp[] array.
  • Now, shift the remaining elements.
  • After, shifting the elements add the elements of temp[] in the array.
Array Rotation In C

Method 1 : Code in C

Run
//Write a program for array rotation in C
#include<stdio.h>
 
/* Driver program to test above functions */
int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60, 70};
    int n = sizeof(arr)/sizeof(arr[0]);
    int k = 3;
    
    int temp[k];
    for(int i=0; i<k; i++)
      temp[i] = arr[i];
    
    int x = k;
    for(int i=0; x < n; i++){
        arr[i] = arr[x++];
    }
    x = 0;
    
    for(int i=n-k; i<n; i++)
       arr[i] = temp[x++];
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}

Output

40 50 60 70 10 20 30

Method 2 :

In this method, we will rotate the elements one by one by shifting them.

Method 2 : Code in C

Run
//Write a program for array rotation in C
#include<stdio.h>
 
void leftRotatebyOne(int arr[], int n)
{
    int temp = arr[0], i;
    for (i = 0; i < n - 1; i++)
        arr[i] = arr[i + 1];
    arr[n-1] = temp;
}

void leftRotate(int arr[], int k, int n)
{
    for (int i = 0; i < k; i++)
        leftRotatebyOne(arr, n);
}
 
/* Driver program to test above functions */
int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60, 70};
    int n = sizeof(arr)/sizeof(arr[0]);
    
    leftRotate(arr, 3, n);
    
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}

Output

40 50 60 70 10 20 30

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java
WordPress › Error

There has been a critical error on this website.

Learn more about troubleshooting WordPress.