#C7895. 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
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.
Detailed Explanation:
Consider an array of length \(n\), where each element is either 0 or 1. You are required to find the maximum length \(L\) such that there exists a contiguous subarray with the same number of 0s and 1s. For instance, in the array [0, 1, 0], the longest such subarray is either [0, 1] or [1, 0], both of which have length 2.
Hint: A common approach is to use a prefix sum strategy by converting 0 to -1 and then finding subarrays with a sum of 0.
inputFormat
The input is given via standard input (stdin) in the following format:
N a1 a2 a3 ... aN
Here, N (1 ≤ N ≤ 10^5) is the number of elements in the array, and each ai
is either 0 or 1.
outputFormat
Output a single integer representing the length of the longest contiguous subarray with an equal number of 0s and 1s. The result is printed to standard output (stdout).
## sample3
0 1 0
2