# Solution to Cyclic-Rotation by codility

9 Feb

Question Name: Cyclic-Rotation or CyclicRotation

With Python’s syntactic sugar, the solution is pretty short.

Well, kind of too easy. Let’s try a C++ solution to demo the details better.

Thanks to @micropentium6, the original C++ solution is ugly and bad. Please refer to the solution from @micropentium6. Also another Python solution with the same method as @micropentium6’s can be found in an older post.

### 33 Replies to “Solution to Cyclic-Rotation by codility”

1. micropentium6 says:

With all due respect, your C++ solution is still like Python tricks. By pushing back, the vector is extended through newly allocated space. You can, actually, do this in place with or without vector.

• Sheng says:

I see~ I forgot this classic algorithm, which was mentioned in some old post. I was in the world of Python ðŸ™‚

• mallesh says:

hi sheng,
INPUT: Array = [3, 8, 9, 7, 6]
k = 3
OUTPUT:[3, 9, 9, 7, 9, 3, 9]

• Sheng says:

I cannot understand your statement. The code should work.

2. Dinar says:

3. bigO says:

Javascript Solution

4. Sundara says:

5. My Javascript solution:

• juan says:

Excelente!!, clean and straighfoward!

6. Paul says:

Here is what I came up with for a C# solution. It scored 100% on Codility:
https://codility.com/demo/results/training4T4DUN-KVN/

7. Hello, I am trying to understand your solution and I have to question that I do not find the logic. Please could illustrate me about it or where I can read more details about your code.
The questions are comment on the solution. Thank you in advances
def solution(A, K):
# write your code in Python 2.7
if len(A) == 0:# why do you equal to cero len(A)?
return A
K = K % len(A)# Why do you module K % len(A)
return A[-K:] + A[:-K]

• Sheng says:

Hello, they are mostly some mathematic issues.
>> why do you equal to cero len(A)?
A: handle the special case. Otherwise, when len(A) is 0, “K = K % len(A)” is illegal.
>> Why do you module K % len(A)
A: To make sure K is in the range of valid index (between 0 and len(A) – 1). The original question is rotation, so if we increase or decrease K by len(A), the result is totally the same.

8. Karol PoczÄ™sny says:

My C solution:

9. Davide says:

My Java solution (100%)

10. Michael says:

Do we really need to shift array n times ? Instead we only need to figure out what is the index of the array would become the first element of new array, and then copy old array to new array. The following are my java solution.

• Sheng says:

No, n-times shift is totally a waste. FYI, your solution needs additional O(N) space, which is not necessary. Please refer to @micropentium6 solution.

11. this scored 100% too

• Sheng says:

12. Alok says:

My Python solution

13. My soulition in php, got 100%:

• Hi Ahmed,
Cannot we use array functions to get the solution?

14. Alem OsmiÄ‡ says:

My C++ solution

15. Tan says:

My Solution (Python)

16. cipto says:

My C# Code 100% works

17. Vipul Gaur says:

Here is python solution using Reversal Algorithm:

18. Emmanuel Okiria says:
```def solution(A, K):
# write your code in Python 3.6
x = 1
while x  0:
A.insert(0, A[-1])
x += 1
A.pop(-1)
return(A)```
19. Marc Lester Tang says: