#include
using namespace std;
int main() {
int tc;
cin>>tc;
for(int i=0;i<tc;i++){
int n;
cin>>n;
int a[n],b[n+1];
b[0]=0;
for(int j=0;j<n;j++){
cin>>a[j];
b[j+1]=b[j]+a[j];
b[j+1]=b[j+1]%n;
}
int count=0;
for(int j=0;j<=n;j++){
for(int k=j+1;k<=n;k++){
if(b[j]==b[k])
count++;
}
}
cout<<count<<endl;
}
return 0;
}
1 case wrong for time limit
Hello @akshitmehta
There are two problems in your code:
TLE is due to nested for loops inside the the for loop of T.
Reason:
for T=10, N=100000
Total iterations will be:
O(TNN)=O(10^11)
Solution:
Watch the video CPP - Divisble Subarrays, Pigeonhole Problem of your course to solve this problem.
Hope, this would help.
Give alike if you are satisfied.
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.