Show problems as
Classic problem #1493MediumArrayDynamic ProgrammingSliding Window

Longest Subarray of 1's After Deleting One Element

Real-world version: Longest Green Run After One Removal
Tracked in this browser only
Read Longest Subarray of 1's After Deleting One Element on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A turnstile writes one entry per minute into log: 1 when it answered the gate controller that minute and 0 when it did not.

Before the log is filed, exactly one entry must be struck out -- the operator is required to remove one minute, not allowed to skip the removal. The auditor then reads the resulting array and reports the length of its longest run of consecutive 1s.

Return the largest length the auditor can be made to report. If no 1 can survive the removal, return 0.

Examples

Example 1

Input
log = [1, 1, 0, 1, 1, 1]
Output
5

Striking out the entry at index 2 leaves five 1s in a row.

Example 2

Input
log = [1, 1, 1, 1]
Output
3

Every minute answered, and one entry still has to go, so three remain in a row.

Example 3

Input
log = [1, 1, 0, 0, 1, 1, 1]
Output
3

Striking out index 2 leaves [1, 1, 0, 1, 1, 1], whose longest run of 1s is three.

Example 4

Input
log = [0, 0, 0]
Output
0

The log holds no 1 at all, so no run of 1s can survive.

Constraints

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

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def longest_subarray(log: list[int]) -> int:
Java
public int longestSubarray(int[] log)