Balanced Subarray
Given an array consisting of only 0s and 1s, find the length of the largest contiguous subarray that has an equal number of 0s and 1s.
[ 0, 1 ]
Explanation. The only subarray with equal number of 0s and 1s is the entire array.
[ 0, 1, 0 ]
Explanation. The subarray [0, 1] or [1, 0] has an equal number of 0s and 1s.
[ 0, 1, 0, 1 ]
Explanation. The entire array has equal number of 0s and 1s.
[ 0, 0, 1, 1, 0 ]
Explanation. The subarray [0, 1, 1, 0] has an equal number of 0s and 1s.
[ 1, 1, 1, 1 ]
Explanation. There is no subarray with equal number of 0s and 1s.
Follow-up: How would your solution change if the input could contain any integers, not just 0s and 1s?
The input array will contain at least one element and a maximum of 10,000 elements. All array elements will be either 0 or 1.
- Views
- 4