If you ant to know more logical programming please check Logical Programming section on top.
Left rotation of array in c# | how to perform left circular rotation of an array
|
Example
Lets say i have an array of {10, 20, 30, 40, 50} then we need to shift one step left.
Input - 10, 20, 30, 40, 50
Output - 20, 30, 40, 50, 10
Logic Behind Left Rotation of Array
10, 20, 30, 40, 50
10, 20, 30, 50, 40 //40 and 50 swapped
10, 20, 40, 50, 30 //40 and 30 swapped
10, 30, 40, 50, 20 //30 and 20 swapped
20, 30, 40, 50, 10 //20 and 10 swapped
How to find Left rotation of array in c# | how to perform left circular rotation of an array in c#?
class Left_Rotation
{
public static void Main()
{
int[] arr = { 10, 20, 30, 40, 50 };
Left_Rotation_Method(arr);
Console.ReadKey();
}
public static void Left_Rotation_Method(int[] arr)
{
int tempval;
for (int i = arr.Length - 1; i > 0; i--)
{
tempval = arr[arr.Length - 1];
arr[arr.Length - 1] = arr[i - 1];
arr[i - 1] = tempval;
}
foreach (var item in arr)
{
Console.Write(item + " ");
}
}
}
Output
Output of Left rotation of array in c# |
No comments:
Post a Comment