#K67037. Longest Subarray of 1s After Flipping
Longest Subarray of 1s After Flipping
Longest Subarray of 1s After Flipping
You are given a binary string consisting of characters '0'
and '1'
. You are allowed to flip at most one '0'
to a '1'
. Your task is to find the length of the longest contiguous subarray of '1'
s possible after performing at most one flip.
More formally, given a binary string \( s \) of length \( n \), you can change at most one character \( s[i] \) from '0'
to '1'
. Determine the maximum length \( L \) such that there exists an index range \( [l, r] \) (with \( 0 \leq l \leq r < n \)) where all characters in the range are '1'
after the flip. The problem can be solved using a sliding window approach in \( O(n) \) time.
inputFormat
The input consists of a single line containing a binary string \( s \) composed solely of characters '0'
and '1'
.
outputFormat
Output a single integer which is the length of the longest contiguous subarray of '1'
s that can be obtained after flipping at most one '0'
to '1'
.
1101110
6