Divisible sub arrays giving Run error

#include
using namespace std;
#define ll long long
long a[1000001],p[1000001];
int main()
{
ll t,sum,ans,n,i;
cin>>t;
while(t–)
{
p[0]=1;
cin>>n;
for(i=1;i<=n;i++)
p[i]=0;
sum=0;
for(i=1;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
p[(sum+n)%n]++;
}
ans=0;
for(i=0;i<=n;i++)
{
ans+=(p[i]*(p[i]-1))/2;
}
cout<<ans<<endl;
}
return 0;
}

@raj.bmp.333 I think run time error in your code is due to array indexes out of bounds. For an array of n elements, last element is at index n-1. You have considered it till n in the for loop. Also you did not take input a[0] and have started from a[1]. Dry run and recheck your for loops and the statements within it. You will get the cause of runtime error. If still you are not able to debug, please copy and save the code in coding blocks ide and share its link.

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.

1 Like