#C9124. Maximum Length Zero Sum Sublist

    ID: 53183 Type: Default 1000ms 256MiB

Maximum Length Zero Sum Sublist

Maximum Length Zero Sum Sublist

You are given t test cases. For each test case, you are provided with an integer \( n \) representing the number of towns and a sequence of \( n \) integers which denote the population of each town. Your task is to find the length of the longest contiguous subarray whose sum is exactly zero.

Explanation: A contiguous subarray is a sequence of consecutive elements from the original array. If there exists a subarray whose sum is zero, you should output its length. Otherwise, output \(0\). The answer for each test case should be printed on a new line.

The problem can be mathematically represented as: Find the maximum \( L \) such that there exists indices \( i \) and \( j \) (with \( 0 \leq i \leq j < n \)) for which \[ \sum_{k=i}^{j} a_k = 0 \] and \( L = j - i + 1 \).

inputFormat

The first line contains an integer \( t \), the number of test cases. Each test case consists of two lines:

  • The first line contains an integer \( n \) denoting the number of elements in the list.
  • The second line contains \( n \) space-separated integers representing the population of each town.

outputFormat

For each test case, output a single integer in a new line denoting the length of the longest contiguous subarray that sums to zero. If such a subarray does not exist, output \(0\).

## sample
1
5
1 -1 2 -2 3
4

</p>