#K94927. Maximum Balanced Subarray

    ID: 38750 Type: Default 1000ms 256MiB

Maximum Balanced Subarray

Maximum Balanced Subarray

Given an array of integers consisting of only 0's and 1's, find the maximum length of a contiguous subarray with an equal number of 0's and 1's.

In other words, you need to determine the length of the longest segment in the array where the number of 0's equals the number of 1's.

Let ( n ) be the number of elements in the array. The problem requires finding a subarray of maximum length such that if you denote ( count(1) ) as the number of ones and ( count(0) ) as the number of zeros in the subarray, then ( count(1) = count(0) ).

inputFormat

The input is given via standard input (stdin).

The first line contains a single integer ( n ), the number of elements in the array. The second line contains ( n ) space-separated integers, each either 0 or 1.

outputFormat

Output a single integer representing the maximum length of a contiguous subarray that contains an equal number of 0's and 1's. The output should be written to standard output (stdout).## sample

3
0 1 0
2