#C13615. Longest Contiguous Subarray with Equal 0s and 1s
Longest Contiguous Subarray with Equal 0s and 1s
Longest Contiguous Subarray with Equal 0s and 1s
Given a binary array nums
(i.e., an array consisting only of 0s and 1s), find the length of the longest contiguous subarray with an equal number of 0s and 1s.
Hint: You can transform the array by replacing 0 with -1 and then find the maximum length subarray whose sum is 0. Mathematically, if we let \( count = (\#ones) - (\#zeros) \), then we need to find indices \( i, j \) such that \( count[j] - count[i] = 0 \), hence \( count[j] = count[i] \).
inputFormat
The input is given in a single line. The first integer N
represents the number of elements in the array, followed by N
space-separated integers (each either 0 or 1).
outputFormat
Output a single integer representing the length of the longest contiguous subarray with equal number of 0s and 1s.
## sample7
0 1 0 1 0 1 1
6