-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzero_sum_subarray.cpp
45 lines (40 loc) · 941 Bytes
/
zero_sum_subarray.cpp
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
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
/*
let an array be a[0.....ax......ay....an];
let sum of subarray a[0...ax] = x;
let sum of subarray a[0....ay] = y;
let sum of subarray a[ax+1.....ay] = 0;
now if the sum of this subarray is 0 that means x + 0 = y
so the algorithm to solve:
calculate sum from left to right
if this sum is previously encountered that means zero sum subarray exists
hence return true
else insert each sum to hash
if no zero sum subarray found return false
*/
bool subArrayExists(int arr[], int n)
{
int sum = 0;
unordered_set<int> hash;
for(int i = 0; i < n; i++){
sum += arr[i];
if(sum == 0 || hash.find(sum) != hash.end()) return true;
hash.insert(sum);
}
return false;
}
int main(){
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++){
cin >> arr[i];
}
if(subArrayExists(arr,n)){
cout << "YES\n";
} else{
cout << "NO\n";
}
return 0;
}