Skip to content

Contiguous Array

MediumArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Given a list nums containing only 0s and 1s, return the length of the longest contiguous subarray with an equal number of 0s and 1s. Return 0 if there is none.

nums = [0, 1]                       →  2
nums = [0, 1, 0]                    →  2
nums = [0, 1, 1, 1, 1, 1, 0, 0, 0]  →  6   ([1, 1, 1, 0, 0, 0])

Constraints

  • 1 <= nums.length <= 10^5
  • nums[i] is either 0 or 1.