Divisible subarray.failing testcases .not sure what im doing wrong

int main() {
int t;
cin>>t;

while(t--){
	int n;
	cin>>n;
	int mod[n];  // array tat store csum%n at each point
	 
	for(int i=0;i<n;i++){
		mod[i]=0;
	}
	ll csum=0;// cumilative sum upto a point 
	ll rem;
	ll ans=0;
	mod[0]=1;
	for(int i=0;i<n;i++){
		ll x;
		cin>>x;
		csum+=x;
		rem=csum%n;
		
		mod[rem]+=1;
		
	}
	
	for(int j=0;j<n;j++){
		if(mod[j]>=2){
			ll k=(mod[j]*(mod[j]-1))/2;
          ans+=k;
		}
	}
	
	cout<<ans<<endl;
	ans=0;
	csum=0;
}
return 0;

}

Try checking the corner cases,
Your csum might overflow(incase of long long) so its better to take
csum=(csum+a[i])%n;
Each time, but csum can be negative if a[i] is negative enough, so to handle that, take
csum=(n+(csum+a[i])%n)%n
Then try!!

@vaishnavi20001611 is your doubt clear now?
you can also refer this video by prateek bhaiya
https://youtu.be/p5Cm_r4T1Rw

1 Like