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
 
0
gauravyashwardhansingh00021 hours ago
int sum=0, count=0, i=0, j=0;       unordered_map<int, int>s;              for(i=0; i<n; i++){           sum += nums[i];           if(sum==0)               count++;           if(s.find(sum-0)!=s.end())               count+=s[sum-0];            s[sum]++;       }               return count;
0
shubham211019973 days ago
 public static long findSubarray(long[] arr ,int n) 
   {
      long count=0;
       HashMap<Long,Long>h=new HashMap<>();
       h.put((long)0,(long)1);
       long presum=0;
       for(long i:arr){
           presum+=i;
           if(h.containsKey(presum)){
               count+=(long)(h.get(presum));
               h.put(presum,h.getOrDefault(presum,(long)0)+(long)1);
           }else{
               h.put(presum,h.getOrDefault(presum,(long)0)+(long)1);
           }
       }
       return count;
   }
+1
madhukartemba3 weeks ago
JAVA SOLUTION:
class Solution{
    //Function to count subarrays with sum equal to 0.
    public static long findSubarray(long[] arr ,int n) 
    {
        
        HashMap<Long, Long> map = new HashMap<>();
        
        long count = 0;
        
        long cur_sum = 0;
        
        
        for(int i=0; i<n; i++)
        {
            cur_sum += arr[i];
            
            if(cur_sum==0) count++;
            
            count += map.getOrDefault(cur_sum, 0l);
            
            //System.out.println(count);
            
            map.put(cur_sum, map.getOrDefault(cur_sum, 0l)+1l);
            
        }
        
        return count;
        
    }
}
+2
harshilgupta00993 weeks ago
ll findSubarray(vector<ll> arr, int n ) {
        //code here
        unordered_map<int,int> umap;
        ll presum=0,ans=0;
        umap[presum]=1;
        
        for(auto x:arr){
            presum+=x;
            
            if(umap.find(presum)!=umap.end()){
                ans+=umap[presum];
            }
            umap[presum]++;
        }
        return ans;
    }
+1
kashyapjhon3 weeks ago
C++ Solution Time=(1.55/4.79) MEDIUM:
ll findSubarray(vector<ll> arr, int n ) {       unordered_map<int,int> u;       u.insert({0,1});       ll sum=0,c=0;       for(int i=0;i<n;i++){           sum=sum+arr[i];           auto it= u.find(sum);           if(it!=u.end()){               c=c+it->second;           }           u[sum]++;       }       return c;   }
0
gulashanshashishekhar293 weeks ago
ll count = 0, sum = 0;
unordered_map<ll, int> m;
for (int i = 0; i < n; i++) {
    sum += arr[i];
    count += m[sum]++;
    if (sum == 0) count++;
}
return count;
0
chintubhuarya753 weeks ago
//1.5 second solution
int count=0;   unordered_map<int,int> m;   unordered_map<int,int> :: iterator it;   m[0]++;   int add=0;   for(int i=0; i<n; i++) {       add = add+ arr[i];             it=m.find(add);       if(it!=m.end()) {           count+=it->second;       }       m[add]++;
   }   
   return count;
+2
ansarizia93351 month ago
3.3 sec JAVA Soln(Using HashMap)
T:O(N)
S:O(N)
class Solution{   //Function to count subarrays with sum equal to 0.   public static long findSubarray(long[] ar ,int n)    {       long sum=0,count=0;       HashMap<Long,Long> hs=new HashMap<>();       hs.put(0L,1L);       for(int i=0;i<n;i++)       {sum+=ar[i];       long x=hs.getOrDefault(sum,0L);       count+=x;       hs.put(sum,x+1);       }       return count;   }}
0
itachinamikaze2211 month ago
JAVA 
class Solution{   //Function to count subarrays with sum equal to 0.   public static long findSubarray(long[] arr ,int n)    {       long c=0;       long sum=0;       HashMap<Long, Integer> map= new HashMap<>();       map.put(sum,1);       for(int i=0; i<arr.length; i++)       {           sum=sum+arr[i];           if (map.containsKey(sum))           {               c=c+map.get(sum);               map.put(sum, map.get(sum)+1);           }           else           map.put(sum, 1);       }       return c;   }}
0
tirtha19025681 month ago
class Solution{
   public static long findSubarray(long[] arr ,int n) 
   {
       HashMap<Long,Long>map = new HashMap<>();
       map.put(0l,1l);
       long count = 0;
       long sum = 0;
       
       for(int i=0;i<n;i++)
       {
           sum = sum + arr[i];
           if(map.containsKey(sum)){
               count+=map.get(sum);
               map.put(sum,map.get(sum)+1);
           }else{
               map.put(sum,1l);
           }
       }
       
       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.