Longest subarray with sum 0

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?

insert 0 in ur map with index -1 (before loop).otherwise ur code will fail for cases like
1 2 3 4 5 -15

Why did we insert m[0]=-1 to make it correct?

because ur code was failing where we have considered all element till i.
and if we consider all element till index i. then length will be i+1 right?
so how i+1 will come .
i-mp[0] -> if we put mp[0] = -1
i-(-1)
i+1

if u dont want to do this.
then check prefsum after adding a[i] is zero or not.
if it is zero then update ur lenght with i+1.