Hi,
I am trying to submit the code of divisible subarray but only one test case is passed , i am not getting the what’s wrong with my code , i am taking prefix sum of array and after that taking the modulo with n , and storing the frequency after taking modulo n and at last i am summing up all the values ans = pre[i]*pre[i-1]/2
Only one test case passed
hey @deepu.kumar2222,what you are caluating as ans is correct . while storing frequency of sum initialize freq[0]=1 and then continue your logic it will work fine.
i had done this still not getting the right answer
Code:- #include<bits/stdc++.h> using namespace std; #define ll long ll a[1000005], pre[1000005]; int main() { int t; cin>>t; while(t–) { int n; cin>>n; int temp = 0; for(int i=0;i<n;i++) { cin>>a[i]; temp = temp + a[i]; a[i] = temp; } pre[0] = 1; for(int i=0;i<n;i++) { a[i] = a[i]%n; pre[a[i]]++; } int ans = 0; for(int i=0;i<n;i++) { int m = pre[i]; ans = ans + ((m)*(m-1))/2; } cout<<ans<<endl; } }
@deepu.kumar2222,you have to update following things in your code:
1.since you have multiple cases initialize arrays inside while(t–) loop.
2.pre[] array should have all values zero initially except pre[0].
3.a[i] can be negative so after taking modulo, in next line write a[i]=(a[i]+n)%n;
4.take long long int everywhere.
sir still getting wrong answer on test case 0,1,2,3
@deepu.kumar2222 i hope i have cleared your doubt if you have any else please let me know else mark this doubt as resolved.
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.