Search⌘ K
AI Features

Solution: Contiguous Array

Explore how to solve the problem of finding the longest contiguous subarray containing equal numbers of 0s and 1s by applying hash maps and running prefix counts. Understand the approach of treating 0s as -1 and 1s as +1 to efficiently track prefix sums and return the maximum balanced subarray length in a single pass.

Statement

You are given a binary array nums containing only 0s0s and 1s1s. Your task is to find the maximum length of a contiguous subarray in which the number of 0s0s and 1s1s is exactly equal.

Return the length of the longest such subarray.

Constraints:

  • 11 \leq nums.length 105\leq 10^5

  • ...