Maximum number of consecutive 1 s after flipping all 0 s in a K length subarray | Function to find the maximum number of consecutive 1 's after flipping all zero in a K length subarray ; Initialize variable ; Iterate unil n - k + 1 as we have to go till i + k ; Iterate in the array in left direction till you get 1 else break ; Iterate in the array in right direction till you get 1 else break ; Compute the maximum length ; Return the length ; Driver code ; Array initialization ; Size of array
def findmax ( arr , n , k ) : NEW_LINE INDENT trav , i = 0 , 0 NEW_LINE c = 0 NEW_LINE maximum = 0 NEW_LINE while i < n - k + 1 : NEW_LINE INDENT trav = i - 1 NEW_LINE c = 0 NEW_LINE while trav >= 0 and arr [ trav ] == 1 : NEW_LINE INDENT trav -= 1 NEW_LINE c += 1 NEW_LINE DEDENT trav = i + k NEW_LINE while ( trav < n and arr [ trav ] == 1 ) : NEW_LINE INDENT trav += 1 NEW_LINE c += 1 NEW_LINE DEDENT c += k NEW_LINE if ( c > maximum ) : NEW_LINE INDENT maximum = c NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return maximum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 3 NEW_LINE arr = [ 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE ans = findmax ( arr , n , k ) NEW_LINE print ( ans ) NEW_LINE DEDENT