Longest Subarray of 1s After Deleting One

Practice this coding question on Coding Gurukul.

Problem statement

A binary status log has to lose exactly one entry during compression. After that deletion, the engineer wants the longest remaining consecutive run of successful entries, represented by ones. Your task Delete exactly one element from the binary array and return the longest contiguous subarray of ones in the resulting array. Important details The deletion is mandatory even when every value is already one. Deleting a zero can join the runs on its two sides. The answer can be zero, including when the input contains only one element. Worked example From [1, 1, 0, 1], deleting the zero joins the other entries into three consecutive ones. Therefore the maximum length is 3.

Input format

The first line contains n. The next line contains n space-separated integers; for n = 0 this line may be empty.

Output format

Print the maximum length after one deletion.

Constraints

1 <= n <= 100000; values are 0 or 1.

Sample input

4
1 1 0 1

Sample output

3

Sign in to run or submit a solution in C++, Java or Python.