Skip to content

Instantly share code, notes, and snippets.

@SourceCode
Last active August 31, 2020 04:58
Show Gist options
  • Save SourceCode/5a8306380b54f94c45e5e463fef7687f to your computer and use it in GitHub Desktop.
Save SourceCode/5a8306380b54f94c45e5e463fef7687f to your computer and use it in GitHub Desktop.
CyclicRotation of Array - Codibility - C#
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