Divisible Subarray (Pigeon Hole) principle

In divisible subarray problem why in case of negative sum
we are taking modulus two times? For ex: if sum is -13 and N=5 so if we simply |sum%N| ++ then counter at position 3 will be increased but on taking mod two times we’re increasing counter of (sum%N + N)%N =2

Can someone provide the reason behind this please!

It’s calculated exactly like the mod of a positive numbr. In arithmetic modulo c, we seek to express any x as qc+r, where r must be a non-negative integer.

now try to print -22%5 in your compiler it will give -2, but this is not correct, so the correct aproach is (-2+5)%5 = 3. why , because 5*-5+3=-22;

1 Like

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.