Problem 986
Maximum Length of Subarray With Positive Product
Medium · 20 pts · Array, Greedy, Dynamic Programming
Problem
Find the length of the longest block of consecutive elements whose product is positive. A zero makes any product zero, so blocks cannot contain one. Keep two lengths while scanning: the longest block ending here with a positive product and the longest with a negative product. A positive number extends both; a negative number swaps them; a zero resets both to 0.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print the length of the longest subarray with a positive product.
Constraints
1 <= N <= 100000; -10^9 <= A[i] <= 10^9
Example input
5 0 1 -2 -3 -4
Example output
3
Explanation
In 0 1 -2 -3 -4, the block 1 -2 -3 has product 6, which is positive, and adding -4 would make it negative. So the answer is 3.