I am not able to understand why we initialize the idx 0 as 1 in pre array?
Divisible Subarrays
Hey @nikhil015
it is done to handle all subarray starting from index 0.
for example
consider mod cumulative sum is
1 2 3 0 4 5 6 6.
here if u see 0 is there that means from index 0 ,to index 3 sum is divisible by n.
now if we initialise freq[0]=1 then freq[0] total count will be 2.
and using formula 2*(2-1)/2 = 1 we get 1 which is correct.
but if we dont initialise freq[0] with 1 then freq[0] total count will be 1.
and using formual 1*(1-1)/2 =0 we get 0. which is wrong .
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.