#K60987. Longest Sequence of Ones with One Flip
Longest Sequence of Ones with One Flip
Longest Sequence of Ones with One Flip
You are given an array consisting of 0's and 1's. Your task is to determine the length of the longest contiguous subarray of 1's that can be achieved by flipping at most one 0 to a 1.
More formally, let the input array be \( A = [a_1, a_2, \dots, a_n] \), where \( a_i \in \{0,1\} \). You are allowed to choose at most one index \( i \) where \( a_i = 0 \) and change it to 1. Find the maximum possible length \( L \) of a subarray (i.e. contiguous segment) consisting entirely of 1's after performing at most one such flip.
Example:
- For \( A = [1, 1, 0, 1, 1, 1] \), flipping the 0 results in \( [1, 1, 1, 1, 1, 1] \) with \( L = 6 \).
- For \( A = [1, 0, 1, 1, 0, 1] \), the best you can achieve is \( L = 4 \) by flipping one of the 0's.
inputFormat
The input is read from standard input and consists of two lines:
- The first line contains an integer \( n \) denoting the number of elements in the array.
- The second line contains \( n \) space-separated integers, each being either 0 or 1.
outputFormat
Output a single integer which is the maximum length of a contiguous subarray of 1's that can be achieved by flipping at most one 0.
## sample6
1 1 0 1 1 1
6