Skip to content

Longest Subarray of 1's After Deleting One Element

MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.

Given a binary array nums, you must delete exactly one element. Return the length of the longest non-empty subarray of only 1s in the result, or 0 if there is none.

nums = [1, 1, 0, 1]                  →  3   (delete the 0)
nums = [0, 1, 1, 1, 0, 1, 1, 0, 1]   →  5   (delete index 4)
nums = [1, 1, 1]                     →  2   (you still have to delete one)

Constraints

  • 1 <= nums.length <= 10^5
  • nums[i] is either 0 or 1.