## Codility Cyclic Rotation Solution C#

**Solution:**

public static int[] Solution(int[] A, int K)

{

int l = A.Length;

int start = l - K;

int [] outA = new int [l];

int j = 0;

if (l == 0 || K == 0)

{

return A;

}

if (start < 0)

{

start = -(start - 1);

}

for (int i = start; i < l; i++)

{

outA[j] = A[i];

j++;

}

for (int m = 0; m < start; m++)

{

outA[j] = A[m];

j++;

}

return outA;

}

**And here goes the result:**

Seems like the constraint: "each element of array A is an integer within the range [−1,000..1,000]." have not effect in the solution.

ReplyDeleteI would definitely use LinQ if that's okay:

A.Skip((K-1) % A.Length).Concat(A.Take((K-1) % A.Length))

The modulus controls multiple turnaraounds of the array. The K-1 is cueing so the result example is correct.

BTW: The proposed solution rotate the array in the wrong direction.

@nymarco - Thanks for your input. But I have tested the solution in codility and the result is also pasted.

Deleteclass Solution {

ReplyDeletepublic int[] solution(int[] A, int K) {

// write your code in C# 6.0 with .NET 4.5 (Mono)

for (var i = 0; i < K; i++) {

for (var j = A.Length - 1; j > 0; j--) {

var temp = A[j];

A[j] = A[j-1];

A[j-1] = temp;

}

}

return A;

}

}

this solution scored 100% also