Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class Program
- {
- static void Main()
- {
- var myArray = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
- int K = int.Parse(Console.ReadLine());
- myArray = CyclicRotation(myArray, K);
- Console.WriteLine(string.Join(" ", myArray));
- }
- public static int[] CyclicRotation(int[] myArray, int K)
- {
- //Rotate an array to the right by a given number of steps.
- // eg k= 1 A = [3, 8, 9, 7, 6] the result is [6, 3, 8, 9, 7]
- // eg k= 3 A = [3, 8, 9, 7, 6] the result is [9, 7, 6, 3, 8]
- if (myArray.Length <=1)
- {
- return myArray;
- }
- int[] sumArray = new int[myArray.Length];
- for (int i = 0; i < K; i++)
- {
- int lastElement = myArray[myArray.Length - 1];
- int[] newArray = new int[myArray.Length];
- newArray[0] = lastElement;
- //here you get the new array after the rotate
- for (int j = 1; j < myArray.Length; j++)
- {
- newArray[j] = myArray[j - 1];
- }
- //here you add every single value sumArray
- for (int j = 0; j < sumArray.Length; j++)
- {
- sumArray[j]+=newArray[j];
- }
- myArray = newArray;
- }
- return sumArray;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement