Left rotation of array in c# | how to perform left circular rotation of an array in c#

Here I am going to discuss one of the most frequently asked question in Interview i.e. Left circular rotation of array in c# or How to perform Left circular rotation of an arrayThe only thing we need to do is to shift each position one step left.

If you ant to know more logical programming please check Logical Programming section on top.

Left rotation of array in c# | how to perform left circular rotation of an array

Left rotation of array in c# | how to perform left circular rotation of an array

Left rotation of array in c# | how to perform left circular rotation of an array












Example

Lets say i have an array of {10, 20, 30, 40, 50} then we need to shift one step left.

Input  -  10, 20, 30, 40, 50
Output - 20, 30, 40, 50, 10


Logic Behind Left Rotation of Array


10, 20, 30, 40, 50
10, 20, 30, 50, 40     //40 and 50 swapped
10, 20, 40, 50, 30    //40 and 30 swapped
10, 30, 40, 50, 20   //30 and 20 swapped
20, 30, 40, 50, 10  //20 and 10 swapped

How to find Left rotation of array in c# | how to perform left circular rotation of an array in c#?

class Left_Rotation
    {
        public static void Main()
        {
            int[] arr = { 10, 20, 30, 40, 50 };
            Left_Rotation_Method(arr);
            Console.ReadKey();
        }
        public static void Left_Rotation_Method(int[] arr)
        {
            int tempval;
            for (int i = arr.Length - 1; i > 0; i--)
            {
                tempval = arr[arr.Length - 1];
                arr[arr.Length - 1] = arr[i - 1];
                arr[i - 1] = tempval;
            }
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
        }
    }


Output
Output of Left rotation of array in c#
Output of Left rotation of array in c#




Share:

No comments:

Post a Comment

Contact for Azure Training

Name

Email *

Message *

Subscribe YouTube

Total Pageviews

Popular Posts

Labels

Recent Posts