Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* How many subarrays are there whose sum is equal to zero...It is most frequent prefix sum of the elements..
- 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.
- */
- int main()
- {
- booster()
- ///read("input.txt");
- ll n;
- cin>>n;
- map<ll,ll>m;
- ll ans = n - 1;
- ll sum = 0;
- for(int i = 0;i<n;i++)
- {
- ll a;
- cin>>a;
- sum+=a;
- m[sum]++;
- ans = min(ans,n - m[sum]);
- }
- cout<<ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement