Divisible subarrays run error

This is a problem from the number theory section. I’m getting run-error with the following code - https://ide.codingblocks.com/#/s/16879
Can anyone tell what the problem is?

Actually your code will give run error for negative values because you are doing this
if(sum>=0){
sum %=n ;}
else {sum = (sum + n)%n;
but if sum is less than zero then sum + n may still a negative no. so you have to do sum%=n first and then sum=(sum+n)%n;
for further help , you can take help of this code
https://ide.codingblocks.com/#/s/16990
:))

1 Like

How is it possible that n < 0? Isn’t it the size of input array?

Here is my code which also throws run-error: https://ide.codingblocks.com/#/s/18620
Please clarify my doubt as well.

Actually it is sum not n , written by mistake