Skip to main content
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

Tags

Grind, NeetCode All.
Last modified on August 25, 2026