-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarrayWithZeroSum.cpp
More file actions
40 lines (32 loc) · 1.03 KB
/
subarrayWithZeroSum.cpp
File metadata and controls
40 lines (32 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*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 */
class Solution{
public:
//Complete this function
//Function to check whether there is a subarray present with 0-sum or not.
//we will count prefix sum for each element and store it in map
//return true if prefix sum of any two elements matches or element is zero
bool subArrayExists(int a[], int n)
{
//count prefix sum of each element in sum
int sum=a[0];
unordered_map<int,int> m;
//marking prefix sum of 1st element
m[sum]=1;
for(int i=1;i<n;i++)
{
sum=sum+a[i];
//checking if current prerfix sum alreadsy marked in map or not
if(sum==0 or m[sum] or a[i]==0)
return true;
//if prefix sum isnt matched then mark sum in map as 1
m[sum]=1;
}
return false;
}
};