#C7994. Longest Zero Sum Subarray

    ID: 51926 Type: Default 1000ms 256MiB

Longest Zero Sum Subarray

Longest Zero Sum Subarray

You are given an array of n integers. Your task is to find the length of the longest contiguous subarray whose sum is zero. If no such subarray exists, output -1.

Let \( arr[0 \ldots n-1] \) be the input array. You need to find the largest integer \( L \) such that there exists indices \( i \) and \( j \) with \( 0 \leq i \leq j < n \) and \( \sum_{k=i}^{j} arr[k] = 0 \). If no such subarray exists, return \(-1\).

Input Format: The input is provided via stdin and contains multiple test cases. The first line contains an integer \( T \) denoting the number of test cases. For each test case, the first line contains an integer \( n \) denoting the size of the array, and the next line contains \( n \) space-separated integers representing the elements of the array.

Output Format: For each test case, output the length of the longest zero sum subarray in a new line. If no such subarray exists, output \(-1\).

inputFormat

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

  • The first line has a single integer \( n \) indicating the number of elements in the array.
  • The second line contains \( n \) space-separated integers.

Example:

3
5
1 2 -3 3 -1
4
1 2 3 4
5
4 -4 2 -2 1

outputFormat

For each test case, output a single integer on a new line representing the length of the longest zero sum subarray. If no such subarray exists, output \(-1\).

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

-1 4

</p>