Snippets Collections
class Solution{
    public:
    //Complete this function
    //Function to check whether there is a subarray present with 0-sum or not.
    bool subArrayExists(int arr[], int n)
    {
        int pref_sum=0;
        unordered_set<int> s;
        for(int i=0;i<n;i++)
        {
            pref_sum+=arr[i];
            if(pref_sum==0) return true;
            if(s.find(pref_sum)!=s.end()) return true;
            s.insert(pref_sum);
        }
        return false;
        //Your code here
    }
};
star

Fri Oct 28 2022 14:01:09 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1?utm_source=gfg&utm_medium=article&utm_campaign=bottom_sticky_on_article

#c++ #morgan

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension