-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubarraySumZero.cpp
More file actions
32 lines (30 loc) · 1.02 KB
/
SubarraySumZero.cpp
File metadata and controls
32 lines (30 loc) · 1.02 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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
bool isZeroSub(int arr[],int n) // Time complexity: O(n) Space complexity: O(n)
{
unordered_set<int> set; // create an empty set to store the sum of elements of each
// subarray `A[0…i]`, where `0 <= i < n`
int sum=0;
set.insert(0); // insert 0 into the set to handle the case when subarray with
// zero-sum starts from index 0
for(int i=0;i<n;i++)
{
sum+=arr[i];
if(set.find(sum) != set.end()) // if the sum is seen before, we have found a subarray with zero-sum
{
return true;
}
else
set.insert(sum);
}
return false;
}
int main()
{
int arr[]={-2,-5,8,8,-8,2};
int n = sizeof(arr)/sizeof(arr[0]);
(isZeroSub(arr,n)) ?
cout<<"Subarray with zero present":
cout<<"subarray with zero absent";
}