Longest Balanced Sub-Sequence
InterviewTime limit1sMemory limit128 MB
Find the length of the longest contiguous block with equal counts of positive and negative numbers.
- Level
Medium4 of 10
- Topics
- Prefix sum, Hash map
- Solved
- No attempts yet
Problem
A sequence is balanced when the number of positive values in it equals the number of negative values.
Given a sequence, find the length of the longest balanced sub-sequence made of consecutive elements.
Input
The first line contains the number of test cases ().
Each test case takes two lines. The first line contains the length of the sequence (). The second line contains non-zero 32-bit signed integers separated by spaces. If , the second line is empty.
Output
For each test case, print one line in the format Case #x: M, where is the test case number starting from 1 and is the length of the longest balanced sub-sequence of consecutive elements. If no balanced sub-sequence exists, is 0.
Hint
For the first test case, the answer is (-5 1 -7 8) or (1 -7 8 -6). Each holds two positive and two negative values.
For the second test case, the answer is (9 -9 -1 6 -7 -1 2 8 3 1 -2 -1), which holds six positive and six negative values.