2Brute2Best
🔥0

Longest Subarray of 1's After Deleting One Element

Medium·LeetCode #1493 Pro OnlyPattern Transfer
LeetCode
01

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.

Input

A binary array nums.

Output

The length of the longest run of 1s after deleting exactly one element.

Example 1
Input: nums = [1,1,0,1]
Output: 3

Delete the 0 at position 2. The array becomes [1,1,1], which has three 1s in a row.

Example 2
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5

Delete the 0 at position 4. Then [1,1,1] and [1,1] join into five 1s in a row.

Example 3
Input: nums = [1,1,1]
Output: 2

There is no 0 to delete, but you still must delete something. Deleting any 1 leaves two 1s.

Constraints
  • • 1 ≤ nums.length ≤ 10^5
  • • nums[i] is 0 or 1
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“longest subarray”
→ Contiguous, longest → variable window, record after fixing.
“containing only 1s”
→ The window can't have zeros left after the deletion.
“delete one element”
→ One zero may be inside the window (it gets deleted) → at most 1 zero.
03

Approach Complexity Ladder

Brute Force — try deleting each elementO(N²)O(1)

For each index, delete it and scan for the longest run of 1s

Better — runs before and after each indexO(N)O(N)

Precompute 1-runs ending at and starting at each index; join them across the deleted one

Optimal — window with at most one zeroO(N)O(1)

Max Consecutive Ones III with k = 1; answer is window length − 1

Brute Force Intuition

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:

Window grows and shrinks. Expand right; when the window breaks the rule, move left until it is valid again. Works when "bigger window = harder to be valid".
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.