#K6116. Longest Contiguous Subarray with Zero Sum
Longest Contiguous Subarray with Zero Sum
Longest Contiguous Subarray with Zero Sum
You are given an integer n
and an array arr
of n
integers. Your task is to find the length of the longest contiguous subarray whose sum equals zero.
Example: For n = 8
and arr = [1, 2, -2, 4, -4, 2, -2, 1]
, the longest subarray with a zero sum is of length 6
.
You need to read input from standard input (stdin) and output the result to standard output (stdout).
Hint: You might want to use a prefix sum array and a hash map to store the indices of the sums.
inputFormat
The first line of input contains a single integer n
which denotes the number of elements in the array. The second line contains n
space-separated integers representing the array arr
.
For example:
8 1 2 -2 4 -4 2 -2 1
outputFormat
Output a single integer representing the length of the longest contiguous subarray with a sum of zero.
For the above example, the output should be:
6## sample
8
1 2 -2 4 -4 2 -2 1
6
</p>