#K66877. Longest Equal Positive Negative Subarray

    ID: 32518 Type: Default 1000ms 256MiB

Longest Equal Positive Negative Subarray

Longest Equal Positive Negative Subarray

You are given an array of n integers. Your task is to find the length of the longest contiguous subarray that contains an equal number of positive and negative integers.

More formally, let the array be \(a_1, a_2, \dots, a_n\). A subarray \(a_i, a_{i+1}, \dots, a_j\) is said to have equal positive and negative numbers if the count of positive numbers equals the count of negative numbers in that subarray.

Note: Zeroes (if any) are ignored in the balance. Only integers strictly greater than 0 are considered positive and those strictly less than 0 are considered negative.

Your solution should run efficiently.

inputFormat

The input is read from stdin and consists of multiple test cases.

  • The first line contains an integer \(T\), the number of test cases.
  • For each test case:
    • The first line contains an integer \(N\) representing the number of elements in the array.
    • The second line contains \(N\) space-separated integers representing the array elements.

outputFormat

For each test case, output a single line containing the length of the longest subarray with equal number of positive and negative integers, written to stdout.

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

6 2 4 6

</p>