#C13432. Longest Equal Subarray
Longest Equal Subarray
Longest Equal Subarray
Given a binary array consisting only of 0s and 1s, your task is to find the length of the longest contiguous subarray that contains an equal number of 0s and 1s. This problem can be approached by converting every 0 to -1 and then finding the longest subarray with a cumulative sum of 0. In mathematical terms, if we denote the transformed array as \(a_k\) (where 0 is replaced by -1 and 1 remains 1), we are looking for the longest sequence such that:
$$\sum_{k=i}^j a_k = 0$$
Improve your problem-solving skills by implementing an efficient solution that runs in linear time.
inputFormat
The first line contains an integer \(n\) which denotes 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 representing the length of the longest contiguous subarray with an equal number of 0s and 1s. If there is no such subarray, output 0.
## sample7
0 1 0 1 0 1 1
6