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
binusharma258 hours ago
why my output is wrong not getting reason
class Solution:      #Function to check whether there is a subarray present with 0-sum or not.   def subArrayExists(self,a,n):       ##Your code here       #Return true or false       su=0       sublist=set()       for i in range(n):           su=su+a[i]           if su ==0 or su in sublist:               return 'Yes'           else:             sublist.add(su)       return 'No'           
0
gautam2002pandeyjee2 days ago
First read the comments
and then go through codes.
//Here concept is if sum + arr[i] == sum        // It means the value of arr[i] ==0;       // Means subarrays with zero sum is present       int sum = 0;       HashSet<Integer> has = new HashSet<>();       for(int i:arr){           sum += i;           if(sum==0){               return true;           }           else if(has.contains(sum)){               return true;           }           else{               has.add(sum);           }       }       return false;
0
shubham211019973 days ago
static boolean findsum(int arr[],int n)
   {
       HashSet<Integer>h=new HashSet<>();
       int prefix=0;
     for(int i:arr){
         prefix+=i;
         if(prefix==0) return true;
        if(h.contains(prefix)) return true;
        else
        h.add(prefix);
     }
     return false;
   }
0
niravramotiya0034 days ago
def subArrayExists(self,arr,n):
       if 0 in arr:
           return True
       dt={}
       tsum=0
       for x in arr:
           tsum+=x
           if tsum==0 or tsum in dt:
               return True
           dt[tsum]=dt.get(tsum,0)+1
       return False
-1
vcreateforme96 days ago
  bool subArrayExists(int arr[], int n)   {       //Your code here       unordered_map<int,int> m;       int sum=0;      m[0]=1;       for(int i=0;i<n;i++)       {           sum+=arr[i];           if(m[sum]==1)           return true;           m[sum]=1;       }       return false;   }
0
janvijindal051 week ago
int ans=0;      unordered_set<int> st;      for(int i=0;i<n;i++){          ans=ans+arr[i];          if(ans==0)          return true;          st.insert(ans);      }      if(st.size()<n)      return true;            return false;       
0
parikshitgaikwad1721 week ago
bool subArrayExists(int arr[], int n)
    {
        unordered_map<int,int> m;
        int sum = 0;
        bool isZero=false;
        for(int i=0;i<n;i++){
            sum+=arr[i];
            m[sum]++; 
            if(arr[i]==0 || sum==0){
                return true;
            }
        }
        unordered_map<int,int > :: iterator it = 		m.begin();
        while(it!=m.end()){
            if(it->second>=2){
                return true;
            }
            it++;
        }
        return false;
        //Your code here
    }
0
badgujarsachin831 week ago
 bool subArrayExists(int arr[], int n)
    {
        //Your code here
        unordered_set<int> st;
        int sum=0;
        st.insert(0);
        for(int i=0;i<n;i++){
            sum+=arr[i];
            if(st.count(sum)){
                return true;
            }else{
                st.insert(sum);
            }
        }
        return false;
    }
0
nitishkamath635901 week ago
    bool subArrayExists(int arr[], int n)   {    unordered_set<int> st;   int sum = 0;   int len = n;   for (int i = 0; i < len; i++)   {       sum = sum + arr[i];
       if (sum == 0)           return true;                  st.insert(sum);   }   if (st.size() < len)       return true;     return false;
0
siddhant071 week ago
//Hashing based solution using unordered set in O(N) time and O(N) space
bool subArrayExists(int arr[], int n)
    {
        unordered_set<int> st;
        int sum = 0;
        st.insert(0);
        for(int i = 0; i < n; i++){
            sum += arr[i];
            if(st.count(sum)){
                return true;
            }
            else{
                st.insert(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.