You are viewing a single comment's thread. Return to all comments →
If the number of rotations is greater than the number of elements, than this can still throw an IndexOutOfBounds as (i+n-d) is negative.
To solve this one can use a true modulus function like Math.floorMod(i-d, n) which rather than returning the remainder returns a modulus. https://stackoverflow.com/questions/5385024/mod-in-java-produces-negative-numbers
Left Rotation
You are viewing a single comment's thread. Return to all comments →
If the number of rotations is greater than the number of elements, than this can still throw an IndexOutOfBounds as (i+n-d) is negative.
To solve this one can use a true modulus function like Math.floorMod(i-d, n) which rather than returning the remainder returns a modulus. https://stackoverflow.com/questions/5385024/mod-in-java-produces-negative-numbers