nsavov

Untitled

Jun 11th, 2019
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Array_Rotation
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] arr = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.             int rotationsInput = int.Parse(Console.ReadLine());
  12.             // check if rotation is bigger than the array length to make a single rotation
  13.             if(rotationsInput > arr.Length)
  14.             {
  15.                 rotationsInput -= arr.Length;
  16.             }
  17.  
  18.             // make a new array
  19.             int[] newArr = new int[arr.Length];
  20.             // count of numbers that are put from the old to the new array
  21.             int numbersCount = 0;
  22.             // fill the new array with the offset from the old
  23.             for (int i = 0;  i < arr.Length - rotationsInput; i++)
  24.             {
  25.                 newArr[i] = arr[i + rotationsInput];
  26.                 numbersCount++;
  27.             }
  28.  
  29.  
  30.             // iterator for the old array
  31.             int arrIt = 0;
  32.             // fill the rest of the new array with the values skipped in the previous cycle
  33.             for(int j = numbersCount; j < newArr.Length; j++)
  34.             {
  35.                 newArr[j] = arr[arrIt];
  36.                 arrIt++;
  37.             }
  38.  
  39.             // read the new array
  40.             for (int i = 0; i < newArr.Length; i++)
  41.             {
  42.                 Console.Write(newArr[i] + " ");
  43.             }
  44.         }
  45.     }
  46. }
Add Comment
Please, Sign In to add comment