File tree 1 file changed +56
-0
lines changed
Subarray with 0 sum - GFG
1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ // { Driver Code Starts
2
+ // A C++ program to find if there is a zero sum
3
+ // subarray
4
+ #include < bits/stdc++.h>
5
+ using namespace std ;
6
+
7
+
8
+
9
+ // } Driver Code Ends
10
+ class Solution {
11
+ public:
12
+ // Complete this function
13
+ // Function to check whether there is a subarray present with 0-sum or not.
14
+ bool subArrayExists (int arr[], int n)
15
+ {
16
+ unordered_map<int , int > m;
17
+
18
+ int sum = 0 ;
19
+ m[0 ]++;
20
+
21
+ for (int i=0 ;i<n; i++)
22
+ {
23
+ sum += arr[i];
24
+ m[sum]++;
25
+ }
26
+
27
+ for (auto i: m)
28
+ if (i.second > 1 )
29
+ return true ;
30
+
31
+ return false ;
32
+ }
33
+ };
34
+
35
+ // { Driver Code Starts.
36
+ // Driver code
37
+ int main ()
38
+ {
39
+ int t;
40
+ cin>>t;
41
+ while (t--)
42
+ {
43
+ int n;
44
+ cin>>n;
45
+ int arr[n];
46
+ for (int i=0 ;i<n;i++)
47
+ cin>>arr[i];
48
+ Solution obj;
49
+ if (obj.subArrayExists (arr, n))
50
+ cout << " Yes\n " ;
51
+ else
52
+ cout << " No\n " ;
53
+ }
54
+ return 0 ;
55
+ }
56
+ // } Driver Code Ends
You can’t perform that action at this time.
0 commit comments