Smallest number greater than or equal to N having sum of digits not exceeding S | Function to calculate sum of digits of n ; Function to find the smallest possible integer satisfying the given condition ; If sum of digits is already smaller than s ; Initialize variables ; Find the k - th digit ; Add remaining ; If sum of digits does not exceed s ; Update K ; Given N and S ; Function call
def sum ( n ) : NEW_LINE INDENT sm = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sm += n % 10 NEW_LINE n //= 10 NEW_LINE DEDENT return sm NEW_LINE DEDENT def smallestNumber ( n , s ) : NEW_LINE INDENT if ( sum ( n ) <= s ) : NEW_LINE INDENT return n NEW_LINE DEDENT ans , k = n , 1 NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT digit = ( ans // k ) % 10 NEW_LINE add = k * ( ( 10 - digit ) % 10 ) NEW_LINE ans += add NEW_LINE if ( sum ( ans ) <= s ) : NEW_LINE INDENT break NEW_LINE DEDENT k *= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT n , s = 3 , 2 NEW_LINE print ( smallestNumber ( n , s ) ) NEW_LINE