LeetCode 525, Medium. Topics: Array, Hash Table, Prefix Sum. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 17 parametrized pytest cases, and a playground notebook:
Problem
Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.
Examples
Constraints
1 <= nums.length <= 10^5
nums[i] is either 0 or 1.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Grind, NeetCode All. Last modified on September 7, 2026