Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2017
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. var myArray = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
  12. int K = int.Parse(Console.ReadLine());
  13.  
  14. myArray = CyclicRotation(myArray, K);
  15. Console.WriteLine(string.Join(" ", myArray));
  16.  
  17. }
  18. public static int[] CyclicRotation(int[] myArray, int K)
  19. {
  20. //Rotate an array to the right by a given number of steps.
  21. // eg k= 1 A = [3, 8, 9, 7, 6] the result is [6, 3, 8, 9, 7]
  22. // eg k= 3 A = [3, 8, 9, 7, 6] the result is [9, 7, 6, 3, 8]
  23.  
  24.  
  25. if (myArray.Length <=1)
  26. {
  27. return myArray;
  28. }
  29.  
  30.  
  31. int[] sumArray = new int[myArray.Length];
  32.  
  33. for (int i = 0; i < K; i++)
  34. {
  35. int lastElement = myArray[myArray.Length - 1];
  36. int[] newArray = new int[myArray.Length];
  37. newArray[0] = lastElement;
  38.  
  39. //here you get the new array after the rotate
  40. for (int j = 1; j < myArray.Length; j++)
  41. {
  42.  
  43. newArray[j] = myArray[j - 1];
  44.  
  45. }
  46. //here you add every single value sumArray
  47. for (int j = 0; j < sumArray.Length; j++)
  48. {
  49. sumArray[j]+=newArray[j];
  50. }
  51.  
  52. myArray = newArray;
  53.  
  54.  
  55. }
  56. return sumArray;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement