Longest Subarray of 1s After Deleting One Element

MediumArray

Description

Given a binary array nums, return the size of the longest non-empty subarray containing only 1s after deleting exactly one element.

Examples

Input:nums = [1,1,0,1]
Output:3
Explanation:

Delete 0 for [1,1,1].

Input:nums = [1,1,1,0,0,1,1,1]
Output:3
Explanation:

Deleting one 0 can only join adjacent runs across a single zero. Here the best run after deleting one 0 has length 3.

Input:nums = [0,0,0]
Output:0
Explanation:

Since the array contains only 0s, deleting one element still leaves no 1s, so the answer is 0.

Constraints

  • 1 ≤ nums.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!