Longest Subarray of 1's After Deleting One Element

Given a binary array `nums`, you must delete exactly one element from it. Return the size of the longest non-empty subarray containing only 1s in the resulting array. Return 0 if there is no such subarray.

30:00

Longest Subarray of 1's After Deleting One Element
medium
Free written solution
Topics
Companies

Given a binary array nums, you must delete exactly one element from it. Return the size of the longest non-empty subarray containing only 1s in the resulting array. Return 0 if there is no such subarray.

Example 1:
Input: {"nums":[1,1,0,1]}
Output: 3
Constraints:
  • 1≤nums.length≤1051 \leq \text{nums.length} \leq 10^5

  • nums[i]\text{nums}[i] is either 00 or 11

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,1,0,1]}

Slide a window over 4 cells that may hold at most one zero; that zero is the element we delete.

binary strip, window holds at most one zero

blue = window

X = deleted

gold = best

1

0

1

1

0

2

1

3

window length after delete

empty window

zeros in window

0 / 1

best

0

Variables
VariableValue
n4
DepthFunction Call
Stack empty
0/11