Introduction
LeetCode 1493: Longest Subarray of 1's After Deleting One Element is a neat sliding window problem that tests your ability to dynamically adjust a window while handling a constraint: deleting exactly one element.
The task is to find the longest subarray of 1's you can get after deleting one element from the array.
This problem is an excellent example of how sliding window with zero counting can convert a potentially brute-force solution into an O(n) linear solution.
If you’d like to try solving the problem first, you can attempt it here:
Try the problem on LeetCode:
https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/
Problem Understanding
You are given:
- A binary array
numscontaining only 0’s and 1’s - You must delete exactly one element
Your task: Return the length of the longest non-empty subarray of 1’s after deleting one element.
Examples:
A naive approach would try removing each element and scanning for the longest subarray →
- Time Complexity: O(n²) → too slow for
nums.lengthup to 10⁵ - Inefficient for large arrays
Key Idea: Sliding Window with At Most One Zero
Notice the following:
- Deleting one element is equivalent to allowing at most one zero in the subarray
- We can use a sliding window
[i, j]and a counterzfor zeros in the window - Expand
jwhilez <= 1 - If
z > 1, shrink the window from the left untilz <= 1 - The length of the window (
j - i) gives the maximum length of consecutive 1’s after deleting one element
Intuition:
- Only one zero is allowed in the window because deleting that zero would turn the window into all 1's
- This converts the problem into a linear sliding window problem with zero counting
Approach (Step-by-Step)
- Initialize
i = 0,j = 0for window pointers - Initialize
z = 0→ number of zeros in current window - Initialize
co = 0→ maximum length of valid subarray - Iterate
jovernums: - If
nums[j] == 0, incrementz - Check
z: - If
z <= 1: window is valid → updateco = max(co, j - i) - If
z > 1: shrink window fromiuntilz <= 1 - Continue expanding the window
- Return
coas the maximum length after deleting one element
Optimization:
- Only need one zero counter and window pointers
- Avoid recomputing subarray lengths repeatedly
Implementation (Java)
Dry Run Example
Input:
Execution:
Window [i, j] | Zeros z | Valid? | Max length co |
| [0,0] → [1] | 0 | Yes | 0 |
| [0,1] → [1,1] | 0 | Yes | 1 |
| [0,2] → [1,1,0] | 1 | Yes | 2 |
| [0,3] → [1,1,0,1] | 1 | Yes | 3 |
Output:
Complexity Analysis
- Time Complexity: O(n) → Each element is visited at most twice (
iandj) - Space Complexity: O(1) → Only counters and pointers used
Edge Cases Considered
- Array of all 1’s → must delete one → max length =
n - 1 - Array of all 0’s → return 0
- Single element arrays → return 0 (because deletion required)
- Zeros at the start/end of array → handled by sliding window
Sliding Window Pattern Importance
This problem is a great example of sliding window with limited violations:
- Maintain a window satisfying a constraint (at most one zero)
- Expand/shrink dynamically
- Compute max length without scanning all subarrays
It’s directly related to problems like:
- Max consecutive ones with k flips
- Longest substring with at most k distinct characters
- Subarray problems with limited replacements
Conclusion
By tracking zeros with a sliding window, we efficiently find the longest subarray of 1’s after deleting one element in O(n) time.
This pattern is reusable in many binary array and string problems, making it a must-know technique for coding interviews.




