#K81182. Longest Balanced Subarray

    ID: 35697 Type: Default 1000ms 256MiB

Longest Balanced Subarray

Longest Balanced Subarray

You are given T test cases. For each test case, you are given a sequence of n integers. Your task is to find the length of the longest contiguous subarray (i.e., a subarray consisting of consecutive elements) whose sum is zero.

A subarray from index i to j (0-indexed) is said to be balanced if \[ \sum_{k=i}^{j} a_k = 0 \]

where (a_k) denotes the (k)-th element of the sequence.

Note: If there is no such subarray, output 0 for that test case.

Example:

Input:
3
5
1 -1 2 -2 3
4
1 2 3 4
6
-1 1 -2 2 -3 3

Output: 4 0 6

</p>

inputFormat

The input is given through standard input (stdin) and consists of multiple test cases. The first line contains an integer T representing the number of test cases. The description of each test case follows:

  • The first line of each test case contains an integer n, the number of elements in the sequence.
  • The second line contains n space-separated integers representing the sequence.

outputFormat

For each test case, output a single line to standard output (stdout) containing a single integer — the length of the longest contiguous subarray whose elements sum to zero. If no such subarray exists, output 0.

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

0 6

</p>