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:2
Explanation:

[0,1] has equal 0s and 1s.

Input:nums = [0,0,1,0,0,1,1,0]
Output:6
Explanation:

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:0
Explanation:

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

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!