Friday, January 15, 2021

Q.8: How to perform Left circular rotation of an array?

 Ans.: The user will input an integer array and the method should shift each element of input array to its Left by one position in circular fashion. The logic is to iterate loop from Length-1 to 0 and swap each element with last element.

  • input: 1 2 3 4 5, output: 2 3 4 5 1
internal static void RotateLeft(int[] array)
{
int size = array.Length;
int temp;
for (int j = size - 1; j > 0; j--)
{
temp = array[size - 1];
array[array.Length - 1] = array[j - 1];
array[j - 1] = temp;
}
foreach (int num in array)
{
Console.Write(num + " ");
}
}

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...