Longest Subarray of 1's After Deleting One Element (LeetCode 1493): Solution and Explanation

Medium
Array
Sliding Window
Dynamic Programming

Given a binary array, you must delete exactly one element. Return the length of the longest run of 1s left in the array afterwards, or 0 if none remains.

Intuition

Deleting a 0 glues the run of 1s on its left to the run on its right. Deleting a 1 only ever shortens a run. So the best deletion is almost always a zero sitting between two runs.

Look at it before the deletion: any answer is a subarray with at most one 0 in it. Remove that 0 and the rest is all ones, so the answer for that subarray is its length minus one.

That turns the problem into a classic window question: find the longest subarray with at most one zero. A sliding window does it in one pass, moving left only when a second zero arrives.

If there is no zero at all, one 1 still has to go. Measuring the window as right - left (length minus one) covers that case without a special branch.

Approach: Sliding window with at most one zero

  1. Set left = 0, zeros = 0 and best = 0.
  2. Move right from 0 to n - 1. If nums[right] is 0, increase zeros.
  3. While zeros > 1, check nums[left]: if it is 0, decrease zeros. Then move left one step right.
  4. The window [left..right] now has at most one zero. Update best = max(best, right - left).
  5. Return best.

Step-by-step example

nums = [1,1,0,1]
  1. right = 0: window [0..0] is a single 1. Deleting one element leaves 0 ones, best = 0.
  2. right = 1: window [0..1] is two 1s. right - left = 1, best = 1.
  3. right = 2: a zero enters, zeros = 1, still allowed. Window [0..2], right - left = 2, best = 2.
  4. right = 3: a 1 enters. Window [0..3] holds one zero; deleting it leaves 3 ones, best = 3.
  5. The scan ends and the answer is 3.

Code: Python, Java, C++

1class Solution:
2    def longestSubarray(self, nums: List[int]) -> int:
3        left = zeros = best = 0
4        for right in range(len(nums)):
5            if nums[right] == 0:
6                zeros += 1
7            while zeros > 1:
8                if nums[left] == 0:
9                    zeros -= 1
10                left += 1
11            best = max(best, right - left)
12        return best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

right visits each index once and left only moves forward, so each index enters and leaves the window at most once.

Space

O(1)

Only the two pointers, the zero count and the best value are stored.

Other approaches

Run lengths on both sides of each zero (time O(n), space O(n))

Precompute, for every index, the run of 1s ending just before it and the run starting just after it. For each zero, the answer candidate is the sum of the two. Handle the all-ones case as n - 1.

Track previous and current runs (time O(n), space O(1))

Walk the array keeping the length of the current run of 1s and the run before the last zero. At each step the candidate is prev + cur. This is the same idea as the window, written as two counters.

Common mistakes

  • Returning n for an all-ones array. One element must always be deleted, so the answer is n - 1.
  • Using the window length right - left + 1 as the answer. The deleted element is inside the window, so subtract one.
  • Moving left by just one step when a second zero arrives. left must slide past the older zero, which can be several cells away.
  • Forgetting that two adjacent zeros split the array completely. The window then restarts right after the first of them.

Frequently asked questions

Why is the answer right - left instead of the window length?

The window may contain one zero, which is the element you delete. If it has no zero, you still delete one 1. Either way, one cell of the window is gone, so the count of 1s left is the length minus one.

How is this related to Max Consecutive Ones III?

It is the same window with k = 1, except that the zero is deleted instead of flipped. So the answer is the Max Consecutive Ones III window length minus one.

What does an array of only zeros return?

0. Every window that holds at most one zero is a single cell, and deleting that cell leaves nothing.