Rotate Array By N From Right or Left

public static Array rotateArrayByNFromRight(int[] ArrayToBeRotated, int N)
{
    int ArrayLength = ArrayToBeRotated.Length;
    if ((N > 0) && (N <= ArrayLength))
    {
                //12345
                Array.Reverse(ArrayToBeRotated, 0, ArrayLength - N);//32145
                Array.Reverse(ArrayToBeRotated, ArrayLength - N, N);//32154
                Array.Reverse(ArrayToBeRotated);//45123
    }
    else
    {
        Console.WriteLine("invalid");
    }
    return ArrayToBeRotated;
}

Left

public static Array rotateArrayByNFromLeft(int[] ArrayToBeRotated, int N)
{
    int ArrayLength = ArrayToBeRotated.Length;
    if ((N > 0) && (N <= ArrayLength))
    {
                //12345
                Array.Reverse(ArrayToBeRotated, 0, N);//21345
                Array.Reverse(ArrayToBeRotated, N, ArrayLength - N);//21543
                Array.Reverse(ArrayToBeRotated);//34512
    }
    else
    {
        Console.WriteLine("invalid");
    }
    return ArrayToBeRotated;
}

results matching ""

    No results matching ""