Advertisement
yanass

Array Rotation

May 30th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 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()
  9.         {
  10.             string[] array = Console.ReadLine().Split().ToArray();
  11.             int numRotations = int.Parse(Console.ReadLine());
  12.  
  13.             string[] rotatedArray = new string[array.Length];
  14.             if (numRotations > array.Length)
  15.                 numRotations -= array.Length;
  16.  
  17.             for (int i = 0; i < array.Length; i++)
  18.             {
  19.                 if (i < numRotations)
  20.                     rotatedArray[array.Length + i - numRotations] = array[i];
  21.                 else
  22.                     rotatedArray[i - numRotations] = array[i];
  23.             }
  24.  
  25.             string rotatedResult = string.Join(' ', rotatedArray);
  26.             Console.WriteLine(rotatedResult);
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement