Getting a run time error on this divisible sub array code is given

#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–)
{
long long n,ans=0;
cin>>n;
vectorv(n),pre(n+1,0),c(n,0);
for(int i=0;i<n;i++)
{
cin>>v[i];
}
for(int i=0;i<n;i++)
{
pre[i+1]=pre[i]+v[i];

	}
	for(int i=0;i<=n;i++)
	{
		pre[i]%=n;
	}
	for(int i=0;i<=n;i++)
	{
		c[pre[i]]++;
	}
	for(int i=0;i<n;i++)
	{
		ans+=((c[i]*(c[i]-1))/2);
	}
	cout<<ans<<endl;
}
return 0;

}

Now it’s working https://ide.codingblocks.com/s/572204. You weren’t performing modular arithmetic correctly.

explain why we do p[i+1]=(p[i+1]+n)%n after p[i+1]%=n; it would de great if great

Reason being p[i] can be negative as array can have negative elements.
You can study about it from here https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/tutorial/

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.