Friday, September 6, 2019

Sample C# program to print left rotation of an array

Here is a sample C# program to print the left rotation of an array to k times very Quickly. This program succeeded all test cases in HackerRank challenge.

Program :
static public void Main ()
{
    int[] arr = {1, 4, 5, 6, 2};

    int length = arr.Length;
    int k = 2;

    LeftRotate(arr, length, k);
}
static void LeftRotate(int[] arr, int length, int k)
{
    for (int i = k; i < k + length; i++)
    {
        Console.Write(arr[i % length] + " ");
    }
}

Output :
5 6 2 1 4