We can place digit upto upperbound & absolute difference with previous digit much be atmost K
for i in range ( upper_limit + 1 ) : NEW_LINE INDENT if ( tight and i == upper_limit ) : NEW_LINE INDENT new_tight = 1 NEW_LINE DEDENT else : NEW_LINE INDENT new_tight = 0 NEW_LINE DEDENT DEDENT