Contiguous Array
Given a binary array nums containing only 0s and 1s, return the maximum length of a contiguous subarray that contains an equal number of 0s and 1s.
Open official problem prompt ↗Find the longest contiguous stretch of the array holding exactly as many 1s as 0s.
Track a hiker's altitude where a 1 is a step up and a 0 a step down. Whenever the hiker returns to an altitude reached earlier, the ground covered in between rose and fell equally, so net elevation change is zero.
- Input
- nums = [0, 1, 0]
- Output
- 2
- Why
- The subarray [0,1] (indices 0..1) has one 0 and one 1; so does [1,0] (indices 1..2). Both have length 2, and no longer balanced subarray exists.
1 <= nums.length <= 10^5nums[i] is either 0 or 1