# Cyclic Rotation

Can I get feedback on my code?

Problem:

A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.

For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.

This is one of the lessons on Codility

Here is my solution:

public int[] solution(int[] A, int K) {
// write your code in Java SE 8
int size = A.length;
int[] cyclic = new int [size];
for (int x=0; x<size; x++) {
cyclic[(x + K) % size] = A[x];
}

return cyclic;
}


You have to do a modulo division for each element of your array. An alternative would be to split it into two subsequent loops:

int size = A.length;
int[] cyclic = new int[size];
K %= size; // thanks to @Josiah for pointing on the case K>sze
for (int i=0; i<size-K; ++i)
{
cyclic[K + i] = A[i];
}

for (int i=0; i<K; ++i)
{
cyclic[i] = A[size - K + i];
}


Regarding naming see the answer of @Timothy Truckle

• I must say your approach is pretty different to what a normal person would have come up with. It confused me at first until I sat down and did a 'dry run' on it. +1 for thinking from a different angle and letting me see it. – a-ina Jul 31 '17 at 11:00
• Please note that this as written has a bug. You could be passed a value of K which is greater than the size of the array, and crash by going out of bounds. (The fix is simply to do one modulo division of K by size before starting.) – Josiah May 5 '18 at 8:25
• That said, this is preferable as an approach from a runtime efficiency perspective. If developing this for real I would start by thinking like this to see how the control flow fits together. Then I'd replace both loops with the appropriate toSystem.arraycopy() for further speed up. – Josiah May 5 '18 at 8:27
• @Josiah Thanks for point on the case K>size. Sure, arraycopy would be an improvement but I wanted to stay next to the original solution. As it's the solution of a lesson, "keep it simple". – milbrandt May 5 '18 at 19:52

The big issue with it is Naming

Finding good names is the hardest part in programming, so always take your time to think about the names of your identifiers.

## avoid single letter names

In Java the length of identifier names names is virtually unlimited. There is no penalty in any way for long identifier names. So don't be stingy with letters when choosing names.

• Thank you for the quick feedback. At Codility, you're given code skeleton and you just have to write the main code snippet. I understand your point and I am a strong advocate of using proper variable names, but in this case, Codility provided me with the variable name 'A' and 'K'. My code is the one that's written inside the function – a-ina Jul 17 '17 at 20:06
• @a-ina "At Codility, you're given code skeleton" But as this is java after all you are free to change the parameter names as they are local to your code... – Timothy Truckle Jul 17 '17 at 20:09
• yes, you're right. From now on, I'll change these bits too – a-ina Jul 18 '17 at 4:12
• @a-ina but the name of your targte array isn't that good either, should better be rotatedArray or similar. – Timothy Truckle Jul 18 '17 at 7:47

Your code looks nice though I'd change a few small bits related to code formatting.

I'd change:

• int[] cyclic = new int [size] remove space in int [size] so it'll become int[size]
• add a new line under int[] cyclic = ... so you have a bit of separation so it's just a bit better structured
• remove the braces near the for-loop so it doesn't add unnecessary noise to the code.

These are my personal recommendations only though

• Removing braces from loops and conditionals is at best a very contentious recommended. It is a very common source of bugs, especially the sort of bugs that appear as you edit the code. For example Google's Style guide for Java says "Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single statement." (google.github.io/styleguide/javaguide.html#s4.1-braces) – Josiah May 5 '18 at 8:18