Mastering the Longest Subarray of 1’s After Deleting One Element in Python – LeetCode 75 Explained

The Longest Subarray of 1’s After Deleting One Element problem is a frequently asked interview question that tests your understanding of sliding window techniques and careful handling of constraints. The task is simple but tricky: given a binary array, delete exactly one element and return the length of the longest subarray consisting only of 1s.

Longest Subarray of 1's After Deleting One Element

In this blog, we’ll break down the problem, explain it step by step, and implement an optimized Python solution using the sliding window method.

Problem Statement

You are given a binary array nums. You must delete exactly one element from it. Return the size of the longest subarray containing only 1s after deleting one element.

Understanding the Problem

  • A subarray is a contiguous slice of the array.
  • You must delete one element (can be 0 or 1).
  • The challenge is to maximize the count of consecutive 1s after this deletion.

This is very similar to Max Consecutive Ones III but instead of being allowed to flip multiple zeros, here you’re required to remove exactly one element.

Examples

Example 1:

Input: nums = [1,1,0,1]
Output: 3
Explanation: Delete the single zero → longest subarray = [1,1,1]

Example 2:

Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: Delete the zero at index 4 → longest subarray = [1,1,1,1,1]

Example 3:

Input: nums = [1,1,1]
Output: 2
Explanation: Must delete one element → longest subarray = length 2

Python Solution – Step-by-Step Explanation

from typing import List

class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        res, l, prefix = 0, 0, 0
        for r, n in enumerate(nums):
            prefix += n
            # shrink window if more than one zero inside
            while prefix < r - l:
                prefix -= nums[l]
                l += 1
            res = max(res, r - l)  # r - l ensures one deletion
        return res

Step 1: Track counts with sliding window

for r, n in enumerate(nums):
    prefix += n
  • Iterate over array with r as right pointer.
  • prefix tracks the number of ones in the current window.

Step 2: Handle constraint of one deletion

while prefix < r - l:
    prefix -= nums[l]
    l += 1
  • If the number of zeros in the window exceeds 1, shrink the window from the left.
  • We ensure at most one deletion allowed.

Step 3: Update maximum length

res = max(res, r - l)
  • Window length = (r - l) not (r - l + 1) because we must delete one element.

Step 4: Return result

return res
  • Returns the maximum subarray length of consecutive ones after one deletion.

Why This Solution Works

  • The window [l..r] always maintains the constraint: at most one zero inside.
  • r - l correctly reflects the longest subarray length after deleting one element.
  • Efficiently handles large arrays with a single pass.

Time and Space Complexity

MetricValue
Time ComplexityO(n) – each element processed once
Space ComplexityO(1) – only pointers and counters

Edge Cases to Consider

InputOutputExplanation
[1,1,1]2Must delete one element even if all are ones
[0,0,0]0Deleting one zero still leaves only zeros
[1,0,1,1,0,1]3Best subarray after one deletion is length 3

Real-World Applications

  • Data transmission: Find longest streak of successful signals after dropping one bad packet.
  • Quality control: Maximize consecutive non-defective products after removing one faulty unit.
  • Network reliability: Longest uptime sequence after ignoring one downtime.

Conclusion

The Longest Subarray of 1’s After Deleting One Element is a textbook sliding window problem. By maintaining a valid window with at most one zero and tracking its maximum size, we achieve a clean and efficient O(n) solution.

Mastering this problem sharpens your ability to apply window adjustment techniques to interview problems where one constraint must always be enforced.

References

LeetCode – Longest Subarray of 1’s After Deleting One Element

Built In – Sliding Window Algorithm Explained

2 thoughts on “Mastering the Longest Subarray of 1’s After Deleting One Element in Python – LeetCode 75 Explained”

Leave a Comment