I have looked at prateek bhaiya’s answer to this and for that all test cases pass. I have his logic in commented for below, but I don’t get how my logic is wrong:
In his solution: sum = (sum+n)%n is essentially (sum%n + n%n)%n where n%n=0, so it effectively taking the mod twice for sum.
In my logic I need to get the remainder of (sum+val)%size, so I do (sum%size + val%size)%size, just as an extra step I take the mod again while adding to the frequency table.
Sometimes my test cases fail, sometime they dont complete.
#include
using namespace std;
int main() {
int num;
cin>>num;
for(int k=0;k<num;k++)
{
long long int size;
cin>>size;
long long int sum=0;
long long int freq_table[size]{0};
//as cumm_sum of 0 is already found
freq_table[0] = 1;
for(int i=0;i<size;i++)
{
long long int val;
cin>>val;
//sum = sum+val;
//sum = sum%size;
//sum = (sum+size)%size;
sum = sum%size;
val = val%size;
sum = (sum+val)%size;
freq_table[sum%size]++;
}
long long int sum_arrays = 0;
for(int i=0;i<size;i++)
{
sum_arrays = sum_arrays+(freq_table[i]*(freq_table[i]-1)/2);
}
cout<<sum_arrays<<endl;
}
return 0;
}