Longest Subarray of 1s After Deleting One Element

Medium

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:6
Explanation:

Delete one 0 to connect [1,1,1] with [1,1,1], forming [1,1,1,1,1,1] of length 6.

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

All elements are 0. After deleting one 0, this gives [0,0] which has no 1s, so the longest subarray of 1s is 0.

Constraints

  • 1 ≤ nums.length ≤ 10⁵

Ready to solve this problem?

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