Generate a Binary String without any consecutive 0 ' s ▁ and ▁ at ▁ most ▁ K ▁ consecutive ▁ 1' s | Function to construct the binary string ; Conditions when string construction is not possible ; Stores maximum 1 's that  can be placed in between ; Place 0 's ; Place 1 's in between ; Count remaining M 's ; Place 1 's at the end ; Place 1 's at the beginning ; Return the final string ; Driver Code
def ConstructBinaryString ( N , M , K ) : NEW_LINE INDENT if ( M < ( N - 1 ) or M > K * ( N + 1 ) ) : NEW_LINE INDENT return ' - 1' NEW_LINE DEDENT ans = " " NEW_LINE l = min ( K , M // ( N - 1 ) ) NEW_LINE temp = N NEW_LINE while ( temp ) : NEW_LINE INDENT temp -= 1 NEW_LINE ans += '0' NEW_LINE if ( temp == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT for i in range ( l ) : NEW_LINE INDENT ans += '1' NEW_LINE DEDENT DEDENT M -= ( N - 1 ) * l NEW_LINE if ( M == 0 ) : NEW_LINE INDENT return ans NEW_LINE DEDENT l = min ( M , K ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT ans += '1' NEW_LINE DEDENT M -= l NEW_LINE while ( M > 0 ) : NEW_LINE INDENT ans = '1' + ans NEW_LINE M -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE M = 9 NEW_LINE K = 2 NEW_LINE print ( ConstructBinaryString ( N , M , K ) ) NEW_LINE DEDENT