Contiguous Array

Prefix Sums, problem 4 of 7

Contiguous Array

Medium

LC #525

prefix sumhash map

Not attempted yet

Given a binary array nums (only 0s and 1s), return the length of the longest contiguous subarray that contains the same number of 0s and 1s. Return 0 if there is none.

Example 1

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

Example 2

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

Either [0, 1] or [1, 0].

Example 3

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

[1, 1, 1, 0, 0, 0] has three of each.

Constraints

  • 1 ≤ len(nums) ≤ 10⁵
  • nums[i] is 0 or 1

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.