Last active
August 31, 2020 04:58
-
-
Save SourceCode/5a8306380b54f94c45e5e463fef7687f to your computer and use it in GitHub Desktop.
CyclicRotation of Array - Codibility - C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace CyclicRotation | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var S = new Solution(); | |
var A = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; | |
int K = 315; | |
A = S.solution(A, K); | |
Console.WriteLine($"{String.Join(",", A)}"); | |
} | |
} | |
class Solution | |
{ | |
public int[] solution(int[] A, int K) | |
{ | |
var result = new int[A.Length]; | |
int max = A.Length - 1; | |
for (int i = 0; i < A.Length; i++) | |
{ | |
result[this.MapPosition(i, K, max)] = A[i]; | |
} | |
return result; | |
} | |
private int MapPosition(int pos, int rotate, int max) | |
{ | |
for (int i = 0; i < rotate; i++) | |
{ | |
if (pos < max) pos++; | |
else pos = 0; | |
} | |
return pos; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment