#K33187. Longest Contiguous Subarray with Equal Positive and Negative Numbers

    ID: 25032 Type: Default 1000ms 256MiB

Longest Contiguous Subarray with Equal Positive and Negative Numbers

Longest Contiguous Subarray with Equal Positive and Negative Numbers

Given an array of nonzero integers, find the length of the longest contiguous subarray that contains an equal number of positive and negative numbers.

In other words, let (A = [a_1, a_2, \dots, a_n]) be the input array with (a_i \neq 0) for all (i). You need to find the maximum length (L) such that there exists a subarray (A[i \dots j]) (with (1 \le i \le j \le n)) where the count of positive numbers equals the count of negative numbers.

Note: Zero does not appear in the array.

inputFormat

The input is given from standard input and has the following format:

(n)
(a_1\ a_2\ \dots\ a_n)

Where (n) ((1 \le n \le 10^5)) is the number of elements and each (a_i) is a nonzero integer.

outputFormat

Output a single integer to standard output representing the length of the longest contiguous subarray that contains an equal number of positive and negative numbers. If no such subarray exists, output 0.## sample

10
1 -1 2 -2 3 -3 4 4 5 -5
6

</p>