Doubt related to divisible subarrays

int cumSum = 0;
for (int i = 0; i < n; i++) {
cumSum += arr[i];

    // as the sum can be negative, taking modulo twice 
    mod[((cumSum % k) + k) % k]++; 
} 

i am not able to understand line where we r dealing with negative numbers?

@dare_devil_007 as arr[i] can be negative, so cumSum can also be negative!

Ok that I am getting but let’s say cumSum =-2 (k=5)and after passing it through the line we are getting 3 as output.so what is the point of converting negative to positive?

this value is still 2,
we increment mod[2], and not 2

let’s say cumSum = -2;
((cumSum%k)+k)%k
((-2%5)+5)%5
((-2)+5)%5
(3%5) = 3

then how come it be 2

Yeah the value is 3, and we increment for 3.

it is that if we don’t convert it into positive then 3 and -2 would be considered different, whereas actually they are equivalent!
consider this : -2,10, here for k=5 answer is 1, but if you don’t make it positive then your answer comes zero as mod[-2]=1, and mod[3]=1 and mod[0]=1

thanks a lot man for clearing,actually i was bit confused

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.