Subarray with 0 sum | Practice | GeeksforGeeks

Given an array of positive and negative numbers. Find if there is a subarray (of size at-least one) with 0 sum.
Example 1:
Input:
5
4 2 -3 1 6

Output: 
Yes

Explanation: 
2, -3, 1 is the subarray 
with sum 0.
Example 2:
Input:
5
4 2 0 1 6

Output: 
Yes

Explanation: 
0 is one of the element 
in the array so there exist a 
subarray with sum 0.

Your Task:
You only need to complete the function subArrayExists() that takes array and n as parameters and returns true or false depending upon whether there is a subarray present with 0-sum or not. Printing will be taken care by the drivers code.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(n).
Constraints:
1 <= n <= 104
-105 <= a[i] <= 105
 
0
ishubham0109 hours ago
HashSet<Integer> map = new HashSet<>();
int sum = 0;
for(int i=0; i<n; i++) {
    sum = sum + arr[i];
    if(sum==0 || arr[i] == 0 || map.contains(sum)) {
        return true;
    }
    map.add(sum);
 }
 return false;
0
ishubham010
This comment was deleted.
0
anil19j1m2q1 day ago
bool subArrayExists(int arr[], int n)   {      unordered_map<int,int> map;      int sum=0;      for(int i=0;i<n;i++){          sum+=arr[i];          if(sum==0 or map[sum] or arr[i]==0){              return true;          }          map[sum]++;      }      return false;   }
+2
sm21mtech120084 days ago
simple explanation in C++
logic:  
for subarray sum to be zero ,if sum is zero or sum value is repeated.
(if we add array elements from first index,let it be stored in sum.)
(store sum values in map or set)
 
 let array is 9,1,-1
sum=9 ,sum=10 ,sum=9
here 9 is repeated ,so subarray sum is zero 
 
bool subArrayExists(int arr[], int n)
   {
    unordered_map<int,int>m;
    int sum=0;
    for(int i=0;i<n;i++)
    {
        sum+=arr[i];
        if(sum==0)return true;
        if(m.find(sum)!=m.end()) return true;
        m[sum]++;
    }
    return false;
   }
0
nishraut74 days ago
Python code:
 
class Solution:      #Function to check whether there is a subarray present with 0-sum or not.   def subArrayExists(self,arr,n):       ##Your code here       #Return true or false       sum = 0       hashmap = {}       for i in arr:           if i == 0:               return True           sum += i                      if sum == 0:               return True                      if sum not in hashmap.keys():               hashmap[sum] = 1           else:               hashmap[sum]+= 1                                 for i in hashmap.values():           if i > 1:               return True                  return False
0
evansubedi9991 week ago
unordered_map<int,int>m;     int sum=0;     int p=0;     for(int i=0;i<n;i++){         sum=sum+arr[i];         if(sum==p)return true;         if(m.find(sum-p)!=m.end()){             return true;         }         m[sum]++;     }     return false;
0
anujpathare1 week ago
#Total Time Taken: 0.57/1.61
 
 
def subArrayExists(self,arr,n):              hash_set = {}       sum = 0              for i in range(n):           sum += arr[i]           if sum == 0:               return True           if sum not in hash_set:               hash_set[sum] = 1           else:               hash_set[sum] += 1
       for i in hash_set.values():              if i > 1:               return True
       return False
+1
vishalkamble2k181 week ago
THE SIMPLEST SOL. EVER  USING C++😍😍😍
 
bool subArrayExists(int arr[], int n)   {       //Your code here       int pre_sum=0;       unordered_set<int> s;              for(int i=0;i<n;i++){        pre_sum += arr[i];        if(pre_sum == 0)    {        return true;    }        if(s.find(pre_sum) != s.end())  return true;         s.insert(pre_sum);}return false;   }
0
ayushkumarmin201 week ago
unordered_set<int>s; int curr=0;
    for( int i=0;i<n; i++){
        curr+=arr[i];
        if(curr==0)return true;
        if(s.find(curr)!=s.end()) return true;
        else s.insert(curr);
    }return false;
0
ashivamsahu141 week ago
<<CPP Solution >>
class Solution
{
    public:
    bool subArrayExists(int arr[], int n)
    {
        unordered_set<int>s;
        int i,cur_sum=0;
        for(i=0;i<n;i++)
        {
            cur_sum=cur_sum+arr[i];
            if(cur_sum==0)
                return true;
            if(s.find(cur_sum) != s.end())
                return true;
            else
                s.insert(cur_sum);
        }
        return false;
    }
};
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.