Negative Direction Modulo

I want to ask about mod that you took in case of negative numbers in array, if the number in negative is bigger than size of array, like if size of array is 10 and value at an index is -100 then how will we calculate the actual index where we would be in this circular array? If we follow the above formula of (i + arr[i] + N) % N then also we will get a negative number.

@phadnis.anurag In which ever case you are getting negative value by formula (i + arr[i] + N) % N this means that you are counting from the end of array(from right side) and for example if you got -2(hypothetical) by this formula and the array = [1,2,3,4,5] we start counting from right side i.e. index -1 = 5 and index -2 = 4 and so on. So Conclusively if you get a negative value then do this (N+value) to get positive index and correct answer.in this N+value will give us 5-2 i.e. 3 and the correct position will be 4.

Ok so we can do (value + N) till the value becomes positive and get the index. Did I understood it correctly?

@phadnis.anurag Just do (value+N) and you will get positive value in one go.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.