#C10927. Longest Equal Positive Negative Subarray

    ID: 40186 Type: Default 1000ms 256MiB

Longest Equal Positive Negative Subarray

Longest Equal Positive Negative Subarray

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

Note that:

  • Only positive numbers (greater than 0) and negative numbers (less than 0) are considered in the count.
  • Zeros, if present, are ignored since they are neither positive nor negative.

Example:

For the array [1, -1, 2, -2, 3], the longest subarray with equal numbers of positive and negative integers is [1, -1, 2, -2], whose length is 4.

Input / Output: The input is read from standard input (stdin) and the output is written to standard output (stdout).

inputFormat

The first line contains an integer n, representing the number of elements in the array. The second line contains n space-separated integers.

Constraints:

  • 1 ≤ n ≤ 105
  • The array elements can be any integer (both positive and negative).

outputFormat

Print a single integer, which is the length of the longest contiguous subarray that contains equal numbers of positive and negative integers. If no such subarray exists, output 0.

## sample
2
1 -1
2

</p>