Why we took mod of sum with n?

in this video we have first taken sum%n for positive numbers and then (sum+n)%n in case we are getting a negative value. Why can’t we directly do (sum+n)%n and skip the step of sum%=n since they both should give the same answers

@Akshita99 taking sum%=n will bring the sum to > -n in case of negative number so that adding -n after doing this will make sum positive and then we take modulo.if we skip sum%=n it may remain negative after adding +n and hence will give negative value after taking modulo.

okay thank you i got it

1 Like