int longestZeroSumSubaarray(int *a, int n)
{
int prefix_sum = 0;
unordered_map<int, int> m;
int length = 0;
for (int i = 0; i < n; i++)
{
prefix_sum += a[i];
//Border case when only one element is 0 in the array till now
if (a[i] == 0 && length == 0)
{
length = 1;
}
//If prefix_sum already present find length of that subarray
if (m.count(prefix_sum))
{
length = max(length, i - m[prefix_sum]);
}
//If new prefix_sum value is discovered
else
{
m[prefix_sum] = i;
}
}
return length;
}
I have written a slightly different code than shown in the video I am getting correct answer…is there any border case which i need to consider that i have missed or is this code correct?