Subarray with Equal 0s and 1s
Mediumjavapythonccppjavascript
Given a binary array containing only 0 and 1, find the length of the longest contiguous subarray with an equal number of zeros and ones.
Input Format
The first line contains an integer N. The second line contains N space-separated integers, each 0 or 1.
Output Format
Print the maximum length, or 0 if no such subarray exists.
Example 1
Input
6 0 0 1 0 1 1
Output
6
Explanation: The whole array has 3 zeros and 3 ones, so the longest balanced subarray has length 6.
- 1 <= N <= 100000
- A[i] is 0 or 1