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.

## Circular Array Rotation

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

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.