Minimum swaps required to make a binary string divisible by 2 ^ k | Function to return the minimum swaps required ; To store the final answer ; To store the count of one and zero ; Loop from end of the string ; If s [ i ] = 1 ; If s [ i ] = 0 ; If c_zero = k ; If the result can 't  be achieved ; Return the final answer ; Driver code
def findMinSwaps ( s , k ) : NEW_LINE INDENT ans = 0 ; NEW_LINE c_one = 0 ; c_zero = 0 ; NEW_LINE for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT c_one += 1 ; NEW_LINE DEDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT c_zero += 1 ; NEW_LINE ans += c_one ; NEW_LINE DEDENT if ( c_zero == k ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( c_zero < k ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "100111" ; NEW_LINE k = 2 ; NEW_LINE print ( findMinSwaps ( s , k ) ) ; NEW_LINE DEDENT