Given a binary array nums, you should delete one element from it.

Return the size of the longest non-empty subarray containing only 1's in the resulting array. Return 0 if there is no such subarray.

&nbsp;
Example 1:

Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.


Example 2:

Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].


Example 3:

Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	nums[i] is either 0 or 1.


class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        n = len(nums)
        pre, suf = [1]*n, [1]*n
        if nums[0] == 0:pre[0] = 0
        if nums[-1] == 0:suf[-1] = 0
        
        for i in range(1, n):
            if nums[i] == 1 and nums[i-1] == 1:
                pre[i] = pre[i-1] + 1
            elif nums[i] == 0:
                pre[i] = 0
        
        for i in range(n-2, -1, -1):
            if nums[i] == 1 and nums[i+1] == 1:
                suf[i] = suf[i+1] + 1
            elif nums[i] == 0:
                suf[i] = 0
        
        ans = 0
        for i in range(n):
            if i == 0:
                ans = max(ans, suf[i+1])
            elif i == n-1:
                ans = max(ans, pre[i-1])
            else:
                ans = max(ans, pre[i-1] + suf[i+1])
        
        return ans
class Solution {
    public int longestSubarray(int[] nums) {
        List<Integer> groups = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0)
                groups.add(nums[i]);
            if (nums[i] == 1) {
                int count = 0;
                while (i < nums.length && nums[i] == 1) {
                    count++;
                    i++;
                }
                groups.add(count);
                if (i < nums.length && nums[i] == 0)
                    groups.add(0);
            }
        }
        int max = 0;
        if (groups.size() == 1) {
            return groups.get(0) - 1;
        }
        for (int i = 0; i < groups.size(); i++) {
            if (i < groups.size() - 2) {
                max = Math.max(max, groups.get(i) + groups.get(i+2));
            } else {
                max = Math.max(max, groups.get(i));
            }
        }
        
        return max;
    }
}
class Solution {
public:
    int longestSubarray(vector<int>& nums) {
        int x=0,y=0,cnt=0;
        for(int i=0;i<nums.size();i++){
            if(nums[i]==1){
                x++;
            }
            else if(nums[i]==0){
               cnt=max(cnt,x+y);
                y=x;
                x=0;
            }
            if(i==nums.size()-1){
                cnt=max(cnt,x+y);
            }
        }
        if(cnt==nums.size())return cnt-1;
        return cnt;
    }
};
var longestSubarray = function(nums) {
    let l = 0, r = 0;
    let longest = 0;
    // Keep track of the idx where the last zero was seen
    let zeroIdx = null;
    while (r < nums.length) {
        // If we encounter a zero
        if (nums[r] === 0) {
            // If this is the first zero encountered, then set the zeroIdx to the current r index
            if (zeroIdx === null) zeroIdx = r;
            else {
                // If we've already encountered a zero, then set the l index to the zeroIdx + 1,
				// effectively removing that zero from the subarray
                l = zeroIdx + 1;
                // Then update the zeroIdx to the current r index
                // This way there will be, at most, one zero in the subarray at all times
                zeroIdx = r;
            }
        }
        longest = Math.max(longest, r - l);
        r++;
    }
    return longest;
};
Longest Subarray of 1's After Deleting One Element