From 3c6356a97701c231eec693918a59ee3c554aae41 Mon Sep 17 00:00:00 2001 From: Saurav Gore Date: Tue, 14 Jun 2022 20:19:33 +0530 Subject: [PATCH] Added Zero Sum Subarrays.cpp --- HashMaps/Zero Sum Subarrays.cpp | 85 +++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 HashMaps/Zero Sum Subarrays.cpp diff --git a/HashMaps/Zero Sum Subarrays.cpp b/HashMaps/Zero Sum Subarrays.cpp new file mode 100644 index 0000000..ece41e9 --- /dev/null +++ b/HashMaps/Zero Sum Subarrays.cpp @@ -0,0 +1,85 @@ +/* + +Print all subarrays with 0 sum + +Given an array, print all subarrays in the array which has sum 0. + +Examples: + +Input: arr = [6, 3, -1, -3, 4, -2, 2, 4, 6, -12, -7] + +Output: + +Subarray found from Index 2 to 4 +Subarray found from Index 2 to 6 +Subarray found from Index 5 to 6 +Subarray found from Index 6 to 9 +Subarray found from Index 0 to 10 + +*/ + +#include + +using namespace std; + +// Time Complexity --> O(n^2), Space Complexity --> O(1) + +void zeroSubASum1(vector &A, int n) +{ + for (int i = 0; i < n; i++) + { + if (A[i] == 0) + cout << "Subarray found from index " << i << " to " << i << endl; + } + for (int i = 2; i <= n; i++) + { + long long sm = 0; + for (int m = 0; m < i; m++) + sm += A[m]; + if (sm == 0) + cout << "Subarray found from index " << 0 << " to " << i - 1 << endl; + int j = i; + while (j < n) + { + sm -= A[j - i]; + sm += A[j]; + if (sm == 0) + cout << "Subarray found from index " << j - i + 1 << " to " << j << endl; + j++; + } + } +} + +// Time Complexity --> O(n), Space Complexity --> O(n) + +void zeroSubASum2(vector &A, int n) +{ + unordered_map> ump; + long long sm = 0; + for (int i = 0; i < n; i++) + { + sm += A[i]; + if (sm == 0) + cout << "Subarray found from index " << 0 << " to " << i << endl; + if (ump.find(sm) != ump.end()) + { + vector tmp = ump[sm]; + for (auto x : tmp) + cout << "Subarray found from index " << x + 1 << " to " << i << endl; + } + ump[sm].push_back(i); + } +} + +int main() +{ + int n; + cin >> n; + vector A(n); + for (int i = 0; i < n; i++) + cin >> A[i]; + zeroSubASum1(A, n); + cout << endl; + zeroSubASum2(A, n); + return 0; +}