Rotate array elements to left

write a program to rotate array elements to left by the specified number of times.

For example

Input
arr = {9,8,7,6,5,4};

Output
arr = {5,4,9,8,7,6 };

Solution:

using System;

namespace ArrayOperations
{
    class Program
    {
        static void Main()
        {
            int[] arr = {9,8,7,6,5,4};
            var n = 4;

            Console.WriteLine("array");
            foreach (var t in arr)
                Console.WriteLine(t);


            for (var i = 0; i < n; i++)
            {
                int j;
                var first = arr[0];

                for (j = 0; j < arr.Length - 1; j++)
                {
                    arr[j] = arr[j + 1];
                }
                arr[j] = first;
            }

            Console.WriteLine("After rotation");
            foreach (var t in arr)
                Console.WriteLine(t);

            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *