Description
Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.
Examples
Input:
nums = [0,1]Output:
2Explanation:
[0,1] has equal 0s and 1s.
Input:
nums = [0,0,1,0,0,1,1,0]Output:
6Explanation:
The subarray [0,1,0,0,1,1] from index 1 to 6 has 3 zeros and 3 ones, making it the longest contiguous subarray with equal numbers of 0s and 1s.
Input:
nums = [1,1,1,1]Output:
0Explanation:
There are only 1s in the array, so no contiguous subarray can have equal numbers of 0s and 1s. The maximum length is 0.
Constraints
- •
1 ≤ nums.length ≤ 10^5 - •
nums[i] is either 0 or 1