C# Program to Rotate an Array

Array Rotation

C# program for array rotation has been shown here. An array rotation is described as the rotation of array elements to either left or right by k positions.


Example 1: Left rotation

Given array A[] = {10, 20, 30, 40, 50, 60}, k = 2

Rotate elements of A[] by 1 position to the left.

=>A[] = {20, 30, 40, 50, 60, 10}

Again rotate elements of A[] by 1 position to the left.

=>A[] = {30, 40, 50, 60, 10, 20}

So, array A[] after left rotation by 2 positions: {30, 40, 50, 60, 10, 20}



Example 2: Right rotation

Given array A[] = {10, 20, 30, 40, 50, 60}, k = 2

Rotate elements of A[] by 1 position to the right.

=>A[] = {60, 10, 20, 30, 40, 50}

Again rotate elements of A[] by 1 position to the left.

=>A[] = {50, 60, 10, 20, 30, 40}

So, array A[] after right rotation by 2 positions: {50, 60, 10, 20, 30, 40}






1. C# program & output to Rotate an Array

Code has been copied
/*****************************
       alphabetacoder.com
C# program to rotate an array
******************************/

using System;

namespace RotateArray {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int i, j, k, s, d, e;

            // take input
            Console.Write("Enter the size of array: ");
            s = Convert.ToInt32(Console.ReadLine());

            // declare an array
            int[] arr = new int[s];

            // take input
            Console.WriteLine("Enter the elements of array: ");
            for (i = 0; i < s; i++) {
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }

            // take input of the direction of rotation
            Console.WriteLine("Enter the direction of rotation: ");
            Console.WriteLine("1. Left rotation");
            Console.WriteLine("2. Right rotation");
            Console.Write("Your choice >> ");
            d = Convert.ToInt32(Console.ReadLine());

            switch (d) 
            {
                case 1:
                    // take input of the number of position of rotation
                    Console.Write("Enter the number of position to be left shifted: ");
                    k = Convert.ToInt32(Console.ReadLine());
    
                    // left shift the array upto k position
                    for (i = 0; i < k; i++) {
                        // get the first elements
                        e = arr[0];
                        for (j = 0; j < s - 1; j++) {
                            arr[j] = arr[j + 1];
                        }
                        arr[s - 1] = e;
                    }
                    // display the array
                    Console.WriteLine("Output array: ");
                    for (i = 0; i < s; i++) {
                        Console.Write(arr[i] + " ");
                    }
                    break;
                case 2:
                    // take input of the number of position of rotation
                    Console.Write("Enter the number of position to be right shifted: ");
                    k = Convert.ToInt32(Console.ReadLine());
    
                    // right shift the array upto k position
                    for (i = 0; i < k; i++) {
                        // get the first elements
                        e = arr[s - 1];
                        for (j = s - 1; j > 0; j--) {
                            arr[j] = arr[j - 1];
                        }
                        arr[0] = e;
                    }
                    // display the array
                    Console.WriteLine("Output array: ");
                    for (i = 0; i < s; i++) {
                        Console.Write(arr[i] + " ");
                    }
                    break;
                default:
                    Console.WriteLine("Wrong choice!");
                    break;
            }

            // wait for the user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter the size of array: 6

Enter the elements of array:

1

2

3

4

5

6

Enter the direction of rotation:

1. Left rotation

2. Right rotation

Your choice >> 2

Enter the number of position to be right shifted: 2

Output array:

5 6 1 2 3 4


Case 2:

Enter the size of array: 6

Enter the elements of array:

1

2

3

4

5

6

Enter the direction of rotation:

1. Left rotation

2. Right rotation

Your choice >> 1

Enter the number of position to be left shifted: 2

Output array:

3 4 5 6 1 2



2. Time complexity to Rotate an Array


Time Complexity: O(n * k)

Here, $n$ elements in an array are to be rotated by $k$ positions to either left or right.