Friday, September 6, 2019

Sample C# program to print left rotation of an array

Here is a sample C# program to print the left rotation of an array to k times very Quickly. This program succeeded all test cases in HackerRank challenge.

Program :
  1. static public void Main ()
  2. {
  3. int[] arr = {1, 4, 5, 6, 2};
  4.  
  5. int length = arr.Length;
  6. int k = 2;
  7.  
  8. LeftRotate(arr, length, k);
  9. }
  1. static void LeftRotate(int[] arr, int length, int k)
  2. {
  3. for (int i = k; i < k + length; i++)
  4. {
  5. Console.Write(arr[i % length] + " ");
  6. }
  7. }

Output :
5 6 2 1 4

No comments:

Post a Comment