We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

It essentially does the 'rotations' before even assigning the array values in the first place, then uses modular arithmetics:

1) First, (i+k) ensures that each array value is 'moved' k positions to the right. Obviously, this means that towards the end of the array, values will exceed the array size, i+k > n-1, which we cannot allow to happen.

2) Then, by using %n ('mod n'), we ensure that whenever (i+k) exceeds the max array position (n-1), we loop back to position a[0] and continue from there. Essentially this works in the same was as a clock 'starts over' when the hands of the clock reach 12.

It took me well over an hour of intense thinking and googling of modular arithmetics, but I finally understood this solution, and I have to agree with the rest of the commenters: it's absolutely beautiful in its simplicity

## Circular Array Rotation

You are viewing a single comment's thread. Return to all comments →

Here is another Java solution

can i just say...

your 'arr[(i+k)%n]' is GENIUS!

mad respect. thank you for thisss.

how this arr[(i+k)%n]' works in the program

It essentially does the 'rotations' before even assigning the array values in the first place, then uses modular arithmetics:

1) First, (i+k) ensures that each array value is 'moved' k positions to the right. Obviously, this means that towards the end of the array, values will exceed the array size, i+k > n-1, which we cannot allow to happen.

2) Then, by using %n ('mod n'), we ensure that whenever (i+k) exceeds the max array position (n-1), we loop back to position a[0] and continue from there. Essentially this works in the same was as a clock 'starts over' when the hands of the clock reach 12.

Modulo is a tricky mathematical concept, but once you understand how it's used here I think you will agree that this solution is absolutely

BRILLIANT. If you don't know what modulo is, there's a pretty good introduction here: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmeticThanks. We need more people like you that take time to explain logic.

Very creative solution ! I did it with deque but this is awesome ,It helped me to learn a lot !

Simply awesome....

Awesome math

one of most creative solution i have ever seen for this type of problem......a complete genius

you are a genius brother....

Thank you that is very kind of you but I would rather not to be called brother as I am a female programmer. :)

The brilliant way of using modulo-aruthmetic...

Could you possibly explain, or point to another resource, where I can better understand what is going on with

`(i+k)%n`

The element at the i th position moves right k times: (i+k)

When we reach the last index of the array we need to go back to first index and that is what we are doing with %n.

You can have a better understanding by appliying the algorithm on the sample input.

Bravo!

genius.

This is so elegant! Thank you!

Awesome math here !!!!!.

I did this in another way..but achived 17.33 pts only..

Genius

can you explain me how this code works?

Great solution!!!!!

It took me well over an hour of intense thinking and googling of modular arithmetics, but I finally understood this solution, and I have to agree with the rest of the commenters: it's absolutely beautiful in its simplicity

smart solution !!

Very nicely done, vote up :)

That's a genius solution. wow! How are you guys able to think this far? What books do you guys follow?

Salute :)