-
Notifications
You must be signed in to change notification settings - Fork 61
/
largestSubarrayWithZero.cpp
43 lines (42 loc) · 1.08 KB
/
largestSubarrayWithZero.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
// Largest subarray with 0 sum
//
// Given an array having both positive and negative integers. The task is to compute the length of the largest subarray with sum 0.
//
// Example 1:
//
// Input:
// N = 8
// A[] = {15,-2,2,-8,1,7,10,23}
// Output: 5
// Explanation: The largest subarray with
// sum 0 will be -2 2 -8 1 7.
// Your Task:
// You just have to complete the function maxLen() which takes two arguments an array A and n, where n is the size of the array A and returns the length of the largest subarray with 0 sum.
//
// Expected Time Complexity: O(N).
// Expected Auxiliary Space: O(N).
int maxLen(int A[], int n)
{
// Your code here
int sum=0;
int maxi=0;
unordered_map<int,int> mpp;
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;
}