ques link https://hack.codingblocks.com/contests/c/529/266
sol https://ide.codingblocks.com/s/38549
why we are doing sum=sum%n before the step sum=(sum+n)%n .
without it the 2nd test case is not getting passed
Divisible subarrays
1 Like
before that we’re doing sum += arr[i], so we’re taking %n afterwards. You could even have done sum = (sum + arr[i])%n
Problem
Why we are doing sum=sum%n before sum=(sum+n)%n.I understand that the latter is used to calculate the Cs % n for negative numbers.But,why the former is used.What if I don’t use that line.
1 Like
Suppose you have sum=-150 and N=51
according to you sum=(sum+n)%n should be done first, which gives (-150+51)%51 =-48 but it should be positive.
therefore first we perform sum%n to bring it in (less than “< n” range) and if its negative (sum+n)%n converts it into corresponding positive number.
Hope this is clear.
2 Likes