#C11709. Maximum Length Subarray with Equal Number of 0s and 1s

    ID: 41055 Type: Default 1000ms 256MiB

Maximum Length Subarray with Equal Number of 0s and 1s

Maximum Length Subarray with Equal Number of 0s and 1s

You are given an array A of n integers where each element is either 0 or 1. Your task is to find the maximum length of a contiguous subarray in which the number of 0s is equal to the number of 1s.

One way to approach this problem is to transform each 0 into -1. With this transformation, the problem reduces to finding the longest subarray with a sum equal to 0. Formally, define for each element:

$a_i = \begin{cases} -1, & \text{if } A[i]=0 \\ 1, & \text{if } A[i]=1 \end{cases}$

You must determine the maximum length of a subarray for which the sum is equal to 0.

inputFormat

The first line of the input contains an integer n, representing the number of elements in the array. The second line contains n space-separated integers where each integer is either 0 or 1.

outputFormat

Output a single integer — the maximum length of a contiguous subarray that contains an equal number of 0s and 1s.

## sample
4
0 1 0 1
4