Divisible subarray problem

what is the need of adding 0 in cumsum at index 0 ?
without that we can also do that ?

i am thinking like …

step 1: cumsum[0,1,…,n-1]
step2: perform cumsum[i]%n for each i , do implace.
step 3: count the frequency of each no. which is ranging from 0 to n-1
step 4 : for all those freq[i]>=2, do the combination (freq[i] C2; and add then to answer

Hey @sk3657860
Elements can be negative as well So sum can go to 0 and in that case this 0 will be used
Say array is
1 -1 here N=2
So cumsum == 1 0
so here this 0 will be considered with that 0.

1 Like

you didn’t get my doubt

in video , prateek bhaiya has taken the array a[]={1,3,2,6,4}
and he has created the cumsum[ ] ={0,1,4,6,12,16}
so i am asking that what is the need of 0 at index 0 in cumsum array ?

because my array a[ ] is of size 5 . so cumsum [ ] should also be of size 5
it should not be 6

Yup there is no need to create like this but you have to consider this 0 as well for final computations.

1 Like

but without that 0 at index i=0 , we can also solve it (without thinking of pigeonhole principle )

i think to explain with pigeonhole principal , he has incorporated 0 at the starting

1 Like

can you please send me any test case which will fail , if i will not consider that 0 ?

3
1 -1 1try this
Output should be 2

1 Like

yes .
if we don’t consider 0 at index 0 , then answer will be wrong

so the fact converges to the situation that when we have cumsum[i]==0 then we have to consider it separately, in case when i will not consider 0 at index 0

thank you so much for much insightful discussion

1 Like