#K79497. 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 of binary integers (0s and 1s), find the length of the longest contiguous subarray that has an equal number of 0s and 1s. For any contiguous subarray with indices (i) to (j), its length is (L = j - i + 1). You are required to compute the maximum (L) for which the count of 0s is equal to the count of 1s.
Hint: Replace every 0 with -1 so that the problem reduces to finding a subarray with a sum of 0.
inputFormat
The first line of input contains an integer (n) — 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: the length of the longest contiguous subarray with an equal number of 0s and 1s.## sample
6
0 1 0 1 0 1
6
</p>