Problem 502
Longest Ones After Deleting One Element
Medium · 20 pts · Array, Sliding Window
Problem
You are given a binary array. You must delete exactly one element from it (any one, even a 1). After the deletion, find the length of the longest block of consecutive 1s. If the array is all 1s, deleting one of them leaves N - 1. A sliding window that contains at most one 0 solves it in one pass: the answer is the largest window size minus one.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1]. Every value is 0 or 1.
Output
Print the length of the longest run of 1s after deleting one element.
Constraints
1 <= N <= 100000; A[i] is 0 or 1
Example input
4 1 1 0 1
Example output
3
Explanation
In 1 1 0 1, deleting the 0 joins the 1s on both sides into 1 1 1, a run of length 3. That is the best possible.