Longest Balanced Sub-Sequence

Interview

Time limit1sMemory limit128 MB

Summary
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 TT (1≤T≤151 \le T \le 15).

Each test case takes two lines. The first line contains the length of the sequence NN (0≤N≤1000000 \le N \le 100000). The second line contains NN non-zero 32-bit signed integers separated by spaces. If N=0N = 0, the second line is empty.

Output

For each test case, print one line in the format Case #x: M, where xx is the test case number starting from 1 and MM is the length of the longest balanced sub-sequence of consecutive elements. If no balanced sub-sequence exists, MM 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.

Examples1

  1. Example 1

    Input
    2
    8
    -1 -5 1 -7 8 -6 -9 -2
    17
    5 -2 1 3 7 9 -9 -1 6 -7 -1 2 8 3 1 -2 -1
    
    Expected output
    Case #1: 4
    Case #2: 12