Count of 0 s to be flipped to make any two adjacent 1 s at least K 0 s apart | Function to find the count of 0 s to be flipped ; Loop traversal to mark K adjacent positions to the right of already existing 1 s . ; Loop traversal to mark K adjacent positions to the left of already existing 1 s . ; Loop to count the maximum number of 0 s that will be replaced by 1 s ; Driver code
def count ( k , s ) : NEW_LINE INDENT ar = [ 0 ] * len ( s ) NEW_LINE end = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if s [ i ] == '1' : NEW_LINE INDENT for j in range ( i , len ( s ) ) : NEW_LINE INDENT if ( j <= i + k ) : NEW_LINE INDENT ar [ j ] = - 1 NEW_LINE end = j NEW_LINE DEDENT DEDENT i = end NEW_LINE DEDENT DEDENT end = 0 NEW_LINE for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT for j in range ( i , - 1 , - 1 ) : NEW_LINE INDENT if ( j >= i - k ) : NEW_LINE INDENT ar [ j ] = - 1 NEW_LINE end = j NEW_LINE DEDENT DEDENT i = end NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE end = 0 NEW_LINE for j in range ( len ( s ) ) : NEW_LINE INDENT if ( ar [ j ] == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE for g in range ( j , len ( s ) ) : NEW_LINE INDENT if ( g <= j + k ) : NEW_LINE INDENT ar [ g ] = - 1 NEW_LINE end = g NEW_LINE DEDENT DEDENT j = end - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT K = 2 NEW_LINE s = "000000" NEW_LINE print ( count ( K , s ) ) NEW_LINE