public static void Main()
MyArray.RotateArrayUsingReversal(new int[]{} , 0);
MyArray.RotateArrayUsingReversal(new int[]{10} , 0);
MyArray.RotateArrayUsingReversal(null , 0);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , 0);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , -1);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , 7);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , 14);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , 1);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , 2);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , 3);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , 11);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , 12);
MyArray.RotateArrayUsingReversal(new int[]{1, 2, 3, 4, 5, 6, 7} , 13);
public static void RotateUsingAnotherArray(int[] arr, int step)
if (step <= 0 || step % length == 0)
Console.WriteLine("No rotation for step " + step);
Console.Write("Rotation for step " + step + " => ");
if (step > length) step %= length;
int[] result = new int[length];
for (int i = 0; i < length; i++)
copyIndex = (i + step >= length) ? (i + step) - length : i + step;
result[copyIndex] = arr[i];
public static void RotateWithoutAnotherArray(int[] arr, int step)
if (step <= 0 || step % length == 0)
Console.WriteLine("No rotation for step " + step);
Console.Write("Rotation for step " + step + " => ");
if (step > length) step %= length;
for (int j = 0; j < step; j++)
for (int i = 1; i <= length; i++)
if(i+1<length) tmpNext = arr[i + 1];
public static void RotateArrayUsingReversal(int[] arr, int step)
if(arr==null || (arr!=null && arr.Length<=1)){
Console.WriteLine("Null , Empty or Array with single element cannot be rotated");
if (step <= 0 || step % length == 0)
Console.WriteLine("No rotation for step " + step);
Console.Write("Rotation for step " + step + " => ");
if (step > length) step %= length;
Reverse(arr,0,length-1-step);
Reverse(arr,length-step,length-1);
private static void Reverse(int[] arr, int start, int end){
for (int i=start, j=end;i<j;i++,j--){
private static void Print(int[] arr)
Console.Write(val + "\t");