How to cyclically rotate the elements of int array left N times in C#

1 Answer

0 votes
using System;

class Program
{
    static void rotate_array_left_one_time(int[] arr) {
        int first = arr[0];
        int arr_size = arr.Length;
  
        for (int i = 0; i < arr_size - 1; i++) {
            arr[i] = arr[i + 1];
        }
         
        arr[arr_size - 1] = first;
    }
    static void Main() {
        int[] arr = new int[] { 4, 7, 2, 9, 3 };
        int arr_size = arr.Length;
        int n_times = 3;
  
        for (int i = 0; i < n_times; i++)
            rotate_array_left_one_time(arr);
      
        for (int i = 0; i < arr_size; i++) {
            Console.Write(arr[i] + " ");
        }
    }
}



 
  
  
/*
run:
  
9 3 4 7 2 
  
*/

 

 



answered Jul 30, 2021 by avibootz
...