<p>Given a binary array <code>nums</code>, you should delete one element from it.</p>

<p>Return <em>the size of the longest non-empty subarray containing only </em><code>1</code><em>&#39;s in the resulting array</em>. Return <code>0</code> if there is no such subarray.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,0,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1&#39;s.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,1,1,0,1,1,0,1]
<strong>Output:</strong> 5
<strong>Explanation:</strong> After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1&#39;s is [1,1,1,1,1].
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> You must delete one element.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        """
        Returns length of longest subarray containing only 1s after deleting one element

        Parameters:
        -----------
        nums (List[int])
            Binary array (list) of int where each int is either 0 or 1

        Returns:
        --------
        int:
            Length of longest subarray of 1s after deleting one element

        Edge Cases:
        -----------
            - No 1s in array -> output 0
            - All 1s -> length of array - 1
            - Single element array -> output 0
            - Array w

        Plan:
        -----
            1) Identify gaps
            2) Sliding window
            3) Window adjustment

        """
        n = len(nums)

        # Special case: If all are 1s, return length of array minus one (since we must delete one)
        if sum(nums) == n:
            return n - 1
    
        # Special case: If all are 0s, return 0 as we can't have any subarray of 1s.
        if sum(nums) == 0:
            return 0

        # Initialize variables for sliding window
        left = 0 # Left pointer of window
        zero_count = 0 # Count zeros in window
        max_len = 0 # Store max length of subarray of 1s

        # Traverse array using right pointer as sliding window
        for right in range(n):
            # If we encounter zero, increment zero count
            if nums[right] == 0:
                zero_count += 1

            # If >1 zeros in current window, move left pointer
            while zero_count > 1:
                if nums[left] == 0:
                    zero_count -= 1
                left += 1

            # Length of subarray is (right-left+1), but we need delete 1 element
            # So current length (right-left as we assume delete one element
            max_len = max(max_len, right - left)
        
        return max_len
        
206