#C10938. Longest Contiguous Subarray with Equal Number of 0s and 1s
Longest Contiguous Subarray with Equal Number of 0s and 1s
Longest Contiguous Subarray with Equal Number of 0s and 1s
You are given an array consisting only of 0s and 1s. Your task is to determine the length of the longest contiguous subarray that contains an equal number of 0s and 1s.
Formal Description:
Given an array A of length \( n \) where \( A_i \in \{0,1\} \), find the maximum length \( L \) such that there exists a contiguous subarray \( A[i \ldots j] \) where \( j - i + 1 = L \) and the number of 0s is equal to the number of 1s.
Hint: You may transform the array by considering each 0 as -1 and then look for a subarray with sum zero.
inputFormat
The first line contains a single integer \( n \) which is the number of elements in the array.
The second line contains \( n \) space-separated integers, each being either 0 or 1.
outputFormat
Output a single integer, which is the length of the longest contiguous subarray with equal number of 0s and 1s. If no such subarray exists, output 0.
## sample3
0 1 0
2