Zero Sum Subarrays | Practice | GeeksforGeeks

You are given an array arr[] of size n. Find the total count of sub-arrays having their sum equal to 0.

Example 1:
Input:
n = 6
arr[] = {0,0,5,5,0,0}
Output: 6
Explanation: The 6 subarrays are 
[0], [0], [0], [0], [0,0], and [0,0].

Example 2:
Input:
n = 10
arr[] = {6,-1,-3,4,-2,2,4,6,-12,-7}
Output: 4
Explanation: The 4 subarrays are [-1 -3 4]
[-2 2], [2 4 6 -12], and [-1 -3 4 -2 2]


Your Task:
You don't need to read input or print anything. Complete the function findSubarray() that takes the array arr and its size n as input parameters and returns the total number of sub-arrays with 0 sum. 
 
Expected Time Complexity : O(n)
Expected Auxilliary Space : O(n)
 
Constraints:    
1<= n <= 107
-1010 <= arri <= 1010
 
+1
gurprakashsinghug20in 8 hours
    ll findSubarray(vector<ll> arr, int n )    {       unordered_map<int,int> umap;       umap[0]=1;       int presum=0;       int count=0;       for(int i=0;i<n;i++)       {           presum+=arr[i];           if(umap.count(presum)==0)           umap[presum]=1;           else           {                              count+=umap[presum];               umap[presum]++;           }       }       return count;   }
+1
kanishk2512961 day ago
   // simple c++ solution
   
     ll findSubarray(vector<ll> arr, int n ) {
        //code here
        ll res = 0;
        unordered_map<ll,ll> m;
        m[0] = 1;
        ll presum = 0;
        for(int i=0; i<n; i++){
            presum += arr[i];
            res += m[presum];
            m[presum]++;
        }
        return res;
    }
0
karanrajkkr954 days ago
ll findSubarray(vector<ll> arr, int n ) {       map<int,int>m;       int presum=0;       for(int i=0;i<n;i++){           presum+=arr[i];           m[presum]++;       }       int ans=0;       map<int,int> :: iterator it;       for(it=m.begin();it!=m.end();it++){           int c=it->second;           ans+=(c*(c-1))/2;           if(it->first==0){               ans=ans+it->second;           }       }       return ans;   }
0
garg698504 days ago
class Solution{
    public:
    ll findSubarray(vector<ll> a, int n ) {
        ll cnt = 0, sum = 0;
        unordered_map<ll, int> m;
        m[0] = 1;
        for (int i = 0; i < n; i++) {
            sum += a[i];
            cnt+= m[sum];
            m[sum]++;
        }
        return cnt;
    }
};
0
pankajkumardasking555554 days ago
ll findSubarray(vector<ll> arr, int n ) {
        unordered_map<int,int> m;
        int current_sum=0,count=0;
        for(int i=0;i<n;i++){
            current_sum=current_sum+arr[i];
            if(current_sum==0){count++;}
            if(m[current_sum]>0){count=count+m[current_sum]; }
            m[current_sum]++;
        }
        return count;
    }
0
ashivamsahu145 days ago
<<<<CPP Solution>>>>
class Solution
{
    public:
    ll findSubarray(vector<ll> arr, int n ) 
    {
        unordered_map<int,int>m;
        int i,cur_sum=0,count=0;
        for(i=0;i<n;i++)
    	{
    		cur_sum += arr[i];
    		if (cur_sum==0)
    			++count;
    		if(m.find(cur_sum)!=m.end())
    			count += m[cur_sum];
    		m[cur_sum]+=1;
    	}
    	return count;
    }
};
0
ashishsoni19986 days ago
just take pen paper you will defiantly understand.
class Solution:   #Function to count subarrays with sum equal to 0.   def findSubArrays(self,arr,n):              #return: count of sub-arrays having their sum equal to 0       d={}       count=0       cumsum=0       for i in range(n):           cumsum+=arr[i]           if cumsum==0:               count+=1           count+=d.get(cumsum,0)           d[cumsum]=d.get(cumsum,0)+1       return count
 
enjoy the day!😉
0
harshjha51220016 days ago
ll findSubarray(vector<ll> arr, int n ) {       //code here       ll sum = 0;       ll count = 0;       unordered_map<ll,ll>ump;       ump.insert({0,1});       for(int i = 0;i<n;i++)       {           sum+=arr[i];           if(ump.find(sum)!=ump.end())           {               count+=ump[sum];               ump[sum]++;           }           else ump.insert({sum , 1});       }       return count;   }
0
gauravbyte1 week ago
    ll findSubarray(vector<ll> arr, int n ) {
        map <ll,ll> mp;
        ll count=0;
        ll sum =0;
        mp[0] = 1;
        for(int i=0; i<n; i++)
        {
            sum += arr[i];
            if(mp.find(sum) != mp.end())
            {
                count = count + mp[sum];
            }
            mp[sum]++;
        }
        return count;
    }
0
gauravbyte1 week ago
    ll findSubarray(vector<ll> arr, int n ) {
        map <ll,ll> mp;
        ll count=0;
        ll sum =0;
        mp[0] = 1;
        for(int i=0; i<n; i++)
        {
            sum += arr[i];
            if(mp.find(sum) != mp.end())
            {
                count = count + mp[sum];
            }
            mp[sum]++;
        }
        return count;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.