You are viewing a single comment's thread. Return to all comments →
your code is very small and still work fine,
will you explain me how [(i+n-d)%n] is working here. please
actually why you are using % here thats making me confuse
% (modulus) gives you the remainder of division.so 4%5=4,5%5=0 likewise 6%5=1, it helps you to stay inside the limit i.e lesser than the value n (i.e till n-1) eg:if n=5 it goes from 0 to 4.try this simple mod in calc or put it in a loop you will understand.we are getting the input value to the rotated index thats why (-d).if right rotation +d.
It's a great solution but how would someone come up with the idea to use a modulus in this case?