#K94062. Longest Equal Binary Subsequence

    ID: 38558 Type: Default 1000ms 256MiB

Longest Equal Binary Subsequence

Longest Equal Binary Subsequence

Given an array of binary digits (0s and 1s), find the length of the longest contiguous subsequence that contains an equal number of 0s and 1s. This problem can be solved efficiently using a running counter and a hash map. In this approach, we map 0 to -1 and 1 to 1. As we iterate through the array, we keep a running total and use the hash map to store the first occurrence of each total. When the same total is encountered again, it indicates that the counts of 0s and 1s between the two indices are equal. The solution should read input from stdin and output the result to stdout.

inputFormat

The input will be provided via standard input. The first line contains an integer n, representing the number of elements in the binary array. The second line contains n space-separated integers (each either 0 or 1), which represent the array.

outputFormat

The output should be a single integer - the length of the longest contiguous subsequence with an equal number of 0s and 1s, printed to standard output.

## sample
4
0 1 0 1
4