Longest Subarray of 1's After Deleting One Element
Problem Statement
You are given an array nums of 0s and 1s.
You must delete exactly one element from it. After the deletion, return the length of the longest block of consecutive 1s. If there are no 1s left, return 0.
The deletion is not optional: even if the array is all 1s, one of them has to go.
A binary array nums.
The length of the longest run of 1s after deleting exactly one element.
Delete the 0 at position 2. The array becomes [1,1,1], which has three 1s in a row.
Delete the 0 at position 4. Then [1,1,1] and [1,1] join into five 1s in a row.
There is no 0 to delete, but you still must delete something. Deleting any 1 leaves two 1s.
- • 1 ≤ nums.length ≤ 10^5
- • nums[i] is 0 or 1
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each index, delete it and scan for the longest run of 1s
Precompute 1-runs ending at and starting at each index; join them across the deleted one
Max Consecutive Ones III with k = 1; answer is window length − 1
Try every element as the one to delete. For each choice, scan the rest of the array and find the longest run of 1s. Keep the best result.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.