Subarray with 0 sum

PHOTO EMBED

Fri Oct 28 2022 14:01:09 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++ #morgan

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
    }
};
content_copyCOPY

Prefix sum algorithm

https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1?utm_source=gfg&utm_medium=article&utm_campaign=bottom_sticky_on_article