Advertisement
RaFiN_

cf-675C

Jan 2nd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. /* How many subarrays are there whose sum is equal to zero...It is most frequent prefix sum of the elements..
  2.  
  3. By the term prefix I mean cumulative sum.`` Eg: Consider the array 2,-1,3,4,5. The prefix sum array would be 2,1,4,8,13. Consider the following array A=[5,5,5,-10,2,4,-2,3,-7,-5] Let us calculate the prefix/cumulative sum prefix[]=[5,10,15,5,7,11,9,12,5,0] {Note: The last term of prefix[] must be zero since it is mentioned in the question that all the terms of A[] will sum up to 0.} Now let's consider the subarrays with sum equal to zero. The subarrays are: prefix[1] to prefix[3] (0 based indexing) prefix[4] to prefix[8] prefix[9] to prefix[1] Now notice the term just before prefix[1] in the prefix array. Again notice the term at prefix[3]. It is again 5. This shows that the maximum number of repetitions of an element in the prefix[] is the number of maximum subarrays of sum 0.
  4.  
  5. */
  6.  
  7.  
  8. int main()
  9. {
  10.     booster()
  11.     ///read("input.txt");
  12.     ll n;
  13.     cin>>n;
  14.     map<ll,ll>m;
  15.     ll ans = n - 1;
  16.     ll sum = 0;
  17.     for(int i = 0;i<n;i++)
  18.     {
  19.         ll a;
  20.         cin>>a;
  21.         sum+=a;
  22.         m[sum]++;
  23.         ans = min(ans,n - m[sum]);
  24.     }
  25.     cout<<ans;
  26.  
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement