Divisible subarrays problem

in the code of this problem why do we need to write both the things :
sum%=n ; and
sum = (sum+n)%n ;

why isn’t only the line : sum = (sum+n)%n ; sufficient?

Hi @alankrit.agr99
See when sum is negative and more than n then if we just use sum = (sum+n)%n then sum+n is still negative so at end sum will be negative. But if we first use sum%=n then on adding n to it will make it positive. Suppose if sum is -11 and n is 5 then if you just use sum = (sum+n)%n comes out to be -1, but if we first use sum%=n then sum will be 4.

If your doubt is clear then mark it as resolved.

yup i understood it thankyou