Find the maximum length of a contiguous subarray of 1s you can obtain after deleting exactly one element from a binary array.
Given a binary array nums, delete exactly one element from the array and then consider the longest contiguous segment consisting only of 1s in the remaining elements.
Return the length of the longest such segment you can achieve.
Because one element must be deleted, the answer may be 0 if the array has no 1s.
Input Format
- An integer array
numscontaining only0s and1s. - Exactly one element must be removed from the array.
Output Format
- Return a single integer: the maximum length of a contiguous subarray of
1s after deleting one element.
Constraints
1 <= nums.lengthnums[i]is either0or1- Delete exactly one element.
Example 1
Input
nums = [1,1,0,1]
Output
3
Explanation
Delete the zero at index 2. The remaining array is [1,1,1], whose longest contiguous run of 1s has length 3.
Example 2
Input
nums = [0,1,1,1,0,1,1,0,1]
Output
5
Explanation
Delete the zero at index 4 to connect the first three 1s with the next two 1s, forming a run of length 5.
Show 1 more example
Example 3
Input
nums = [1,1,1]
Output
2
Explanation
You must delete one element, so the longest run of 1s becomes 2.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.