Failing testcases

I have coded as per my understanding but all tc are getting failed and not able to detect problem in code.

Code - https://ide.codingblocks.com/s/428018

Check now ->


Passing all test cases.

Why we taking pre[0] =1 ?

You can ignore that, and make rest same. It will still work same.

No, it is not. Commenting that line will fail all testcases

Yes, it’s like for test case:
5 5 5 5 5
value of mod will be 0 every time. So total sum will be 5 in pre[0] if you are not keeping pre[0]=1. So if you don’t keep pre[0]=1 answer will be 5, but expected answer will be 6.
So if any sub-array sum from index i’th to j’th is divisible by k then we can say a[0]+…a[i-1] (mod n) = a[0]+…+a[j] (mod n)

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.