Problem Walkthrough

Longest Subarray 1s After Delete LeetCode 1493

Reframe LeetCode 1493 as a sliding window with at most one zero (k=1), then subtract 1 for the mandatory deletion — the right-left formula elegantly handles both the window size and the required delete in a single step.

8 min read|

Longest Subarray 1s Delete

LeetCode 1493

Problem Overview

LeetCode 1493 — Longest Subarray of 1s After Deleting One Element asks you to take a binary array and delete exactly one element, then return the length of the longest non-empty subarray consisting entirely of 1s. The deletion is mandatory — you must delete one element even if the entire array is already all 1s.

The constraint that you must delete exactly one element is the defining twist of this problem. If deletion were optional, the answer would simply be the longest consecutive run of 1s. But because you must always delete one element — even in an all-1s array — the maximum possible answer is n-1, not n, where n is the array length.

This mandatory-deletion constraint shapes the algorithm in a precise way: you want to find the longest window that contains at most one 0 (which you will delete), then report window_size minus 1 to account for the deleted element. If the window has no zeros, you must still delete a 1 from the window.

  • Given a binary array nums (values 0 or 1 only)
  • You must delete exactly one element — no skipping the deletion
  • After deletion, find the longest non-empty subarray of all 1s
  • Return that length — the result is the count of 1s remaining, not the window size
  • If the whole array is 1s, you must still delete one, so the answer is n-1

Connection to Max Consecutive Ones III

LeetCode 1493 is a direct specialization of Max Consecutive Ones III (LeetCode 1004) with k=1 — with one critical difference: in LeetCode 1004, flipping zeros is optional and you maximize the window as-is. In LeetCode 1493, you must delete one element, so the answer equals the best window size minus 1.

To see why: find the longest contiguous subarray that contains at most one 0. That window has a size of right-left+1. You must delete the 0 inside (or any element if there are no zeros), so the number of 1s remaining after deletion is right-left+1-1 = right-left. This is exactly the right-left formula used in the inner loop.

The reframing is powerful: if you know how to solve Max Consecutive Ones III, you already know how to solve this problem. The only change is the length formula — replace right-left+1 with right-left — and the mandatory deletion is automatically accounted for.

💡

Reframe: Max Consecutive Ones III with k=1, Minus 1

Find the longest window with at most one 0, then subtract 1 because you must delete that 0 (or any element if the array is all 1s). This is Max Consecutive Ones III with k=1 minus 1. The formula right-left handles both the window size and the mandatory deletion in one step — no separate subtract needed at the end.

Sliding Window Algorithm

Initialize left=0, zeros=0, maxLen=0. Iterate right from 0 to len(nums)-1. At each step, if nums[right] equals 0, increment zeros. Then, while zeros exceeds 1, check if nums[left] equals 0 and decrement zeros if so, then increment left. This shrinks the window until it contains at most one zero.

After adjusting the window, update maxLen = max(maxLen, right-left). Note carefully: the update uses right-left, not right-left+1. This is intentional — the window currently spans indices [left, right] which has size right-left+1, but since we delete one element (the zero or a one), the resulting subarray of 1s has length right-left.

The algorithm runs in O(n) time because both left and right only ever move forward — left never passes right, and right advances through the array exactly once. Space is O(1): only the three integer variables left, zeros, and maxLen are needed regardless of input size.

  1. 1Initialize: left=0, zeros=0, maxLen=0
  2. 2For each right in range(len(nums)):
  3. 3 If nums[right] == 0: zeros += 1
  4. 4 While zeros > 1: if nums[left] == 0: zeros -= 1; left += 1
  5. 5 maxLen = max(maxLen, right - left) ← note: right-left, NOT right-left+1
  6. 6Return maxLen

Why right-left Not right-left+1

The standard sliding window length formula is right-left+1 — the window spans indices [left, right] inclusive, so it contains right-left+1 elements. But in LeetCode 1493, we use right-left because we must always delete exactly one element from that window.

When the window contains exactly one 0, we delete that 0. The remaining elements are all 1s and their count is right-left+1-1 = right-left. When the window contains no 0s (all 1s), we still must delete one element (a 1), so the remaining 1s count is again right-left+1-1 = right-left. In both cases, right-left is the correct answer.

This elegant formula handles the mandatory deletion without any special-casing. You do not need to check whether the window has a zero or not — the subtract-1 is already baked into the length formula. Attempting to use right-left+1 will overcount by 1 in all cases, as it ignores the required deletion.

ℹ️

right-left Accounts for the Mandatory Deletion in One Step

Using right-left instead of right-left+1 accounts for the mandatory deletion in one step — no need for a separate subtract-1 at the end. The window size is right-left+1, but you must delete one element, so the answer is right-left+1-1 = right-left. This works whether the deleted element is a 0 or a 1 (when the window is all 1s), making the formula universally correct.

Edge Case: All 1s

If the input array contains no zeros at all, the sliding window never activates the inner while loop — zeros remains 0 throughout, the window expands to the full array, and the final maxLen is (n-1) - 0 = n-1. This correctly reflects that you must still delete one element (a 1) even when all elements are already 1s.

Many solutions attempt to special-case this scenario by checking "if no zeros, return n-1" at the beginning. This is unnecessary — the right-left formula handles it automatically. The window grows to span [0, n-1], so right=n-1 and left=0, giving right-left = n-1-0 = n-1.

Another edge case: a single-element array. The only element (0 or 1) must be deleted, leaving an empty subarray. The problem guarantees the result must be non-empty, so the constraint ensures the input has at least 2 elements in practice. However, if you want to guard against it, return max(0, maxLen) or check len(nums) < 2 and return 0.

  1. 1Array: [1,1,1,1,1] — all 1s, no zeros
  2. 2right advances to 4; zeros stays 0; left stays 0
  3. 3maxLen at right=4: max(0, 4-0) = 4
  4. 4Answer: 4 = n-1 = 5-1 — correct, must delete one 1
  5. 5No special-casing needed — right-left handles it automatically

Code Walkthrough — Python and Java

Python: def longestSubarray(nums): left=0; zeros=0; maxLen=0; for right in range(len(nums)): if nums[right]==0: zeros+=1; while zeros>1: if nums[left]==0: zeros-=1; left+=1; maxLen=max(maxLen, right-left); return maxLen. Single for loop, O(n) time, O(1) space. The key detail is right-left (not right-left+1) in the maxLen update.

Java: public int longestSubarray(int[] nums) { int left=0, zeros=0, maxLen=0; for (int right=0; right<nums.length; right++) { if (nums[right]==0) zeros++; while (zeros>1) { if (nums[left]==0) zeros--; left++; } maxLen=Math.max(maxLen, right-left); } return maxLen; } Identical logic, identical length formula. Both are O(n) time, O(1) space.

Compared to Max Consecutive Ones III (LeetCode 1004), the only change is the length formula: replace right-left+1 with right-left. The sliding window structure, the zeros counter, and the while loop are identical. This makes LeetCode 1493 a one-line modification of the LeetCode 1004 solution once you understand the mandatory deletion.

⚠️

Non-Empty Result: Single-Element Array Returns 0

The problem requires a non-empty result after deletion. If the array has a single element, deleting it leaves nothing — return 0. The mandatory deletion means a single-element array becomes empty, which violates the non-empty constraint. In practice the problem constraints guarantee len(nums) >= 1, but defensively handle it: if len(nums) <= 1, return 0. The standard sliding window handles arrays of length >= 2 correctly without modification.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now