namespace chapter_2
{
class Challenge8
{
//Right Rotate an Array by 1
static void rotateArray(int []arr, int size)
{
//Store the last element of the array.
//Start from the last index and right shift the array by one.
//Make the last element stored to be the first element of the array.
int lastElement = arr[size - 1];
for (int i = size - 1; i > 0; i--)
{
arr[i] = arr[i - 1];
}
arr[0] = lastElement;
}
static void Main(string[] args)
{
int size = 6;
int []arr = { 3, 6, 1, 8, 4, 2 };
Console.Write("Array before rotation: ");
for (int i = 0; i < size; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine("");
rotateArray(arr, size);
Console.Write("Array after rotation: ");
for (int i = 0; i < size; i++)
{
Console.Write(arr[i] + " ");
}
}
}
}