Rotate Array By N From Right or Left
Right
public static Array rotateArrayByNFromRight(int[] ArrayToBeRotated, int N)
{
int ArrayLength = ArrayToBeRotated.Length;
if ((N > 0) && (N <= ArrayLength))
{
Array.Reverse(ArrayToBeRotated, 0, ArrayLength - N);
Array.Reverse(ArrayToBeRotated, ArrayLength - N, N);
Array.Reverse(ArrayToBeRotated);
}
else
{
Console.WriteLine("invalid");
}
return ArrayToBeRotated;
}
Left
public static Array rotateArrayByNFromLeft(int[] ArrayToBeRotated, int N)
{
int ArrayLength = ArrayToBeRotated.Length;
if ((N > 0) && (N <= ArrayLength))
{
Array.Reverse(ArrayToBeRotated, 0, N);
Array.Reverse(ArrayToBeRotated, N, ArrayLength - N);
Array.Reverse(ArrayToBeRotated);
}
else
{
Console.WriteLine("invalid");
}
return ArrayToBeRotated;
}