From 1f7917d47b83f749145405e29affd701338177ee Mon Sep 17 00:00:00 2001 From: shubham Date: Thu, 8 Jun 2023 23:26:37 +0530 Subject: [PATCH] Added Longest Subarray With 0 Sum Problem --- Arrays/Largest_subarray_with_0_sum.cpp | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Arrays/Largest_subarray_with_0_sum.cpp diff --git a/Arrays/Largest_subarray_with_0_sum.cpp b/Arrays/Largest_subarray_with_0_sum.cpp new file mode 100644 index 0000000..f520337 --- /dev/null +++ b/Arrays/Largest_subarray_with_0_sum.cpp @@ -0,0 +1,33 @@ +/*You are required to complete this function*/ + +class Solution +{ +public: + int maxLen(vector &A, int n) + { + unordered_map mpp; + int maxi = 0; + int sum = 0; + for (int i = 0; i < n; i++) + { + sum += A[i]; + if (sum == 0) + { + maxi = i + 1; + } + else + { + if (mpp.find(sum) != mpp.end()) + { + maxi = max(maxi, i - mpp[sum]); + } + else + { + mpp[sum] = i; + } + } + } + + return maxi; + } +}; \ No newline at end of file