Contiguous Array

Medium Topics Company Tags

Find the longest contiguous part of the binary array nums that contains exactly as many zeros as ones. Return its length, or zero when no nonempty subarray qualifies.

Example 1:

Input: nums = [0,1,1,0,1,0]

Output: 6

Explanation: The entire array contains three zeros and three ones, so all six entries form a balanced subarray.

Example 2:

Input: nums = [0,0,1]

Output: 2

Explanation: The last two entries form [0,1], with one zero and one one. The full array is not balanced.

Example 3:

Input: nums = [1]

Output: 0

Explanation: The array contains no zero, so it has no nonempty subarray with equal counts of zeros and ones.

Constraints:

  • 1 <= nums.length <= 100000
  • 0 <= nums[i] <= 1


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.