Minimum value of X that can be added to N to minimize sum of the digits to Ã ¢ â €°¤ K | ''Python program for the above approach ; ''Function to find the minimum number needed to be added so that the sum of the digits does not exceed K ;  ;  ; '' Add the digits of num2 ; ''If the sum of the digits of N  is less than or equal to K ; '' No number needs to  be added ; '' Otherwise ; '' Calculate the sum of digits  from least significant digit ; '' If sum exceeds K ; '' Increase previous  digit by 1 ; '' Add zeros to the end ; '' Calculate difference  between the result and N ; '' Return the result ; ''Driver Code
import math ; NEW_LINE def minDigits ( N , K ) : NEW_LINE ' ▁ Find ▁ the ▁ number ▁ of ▁ digits ' ' ' NEW_LINE digits_num = int ( math . floor ( math . log ( N ) + 1 ) ) ; NEW_LINE INDENT temp_sum = 0 ; NEW_LINE temp = digits_num ; NEW_LINE result = 0 ; NEW_LINE X = 0 ; var = 0 ; NEW_LINE sum1 = 0 ; NEW_LINE num2 = N ; NEW_LINE DEDENT ' Calculate ▁ sum ▁ of ▁ the ▁ digits ' ' ' NEW_LINE INDENT while ( num2 != 0 ) : NEW_LINE sum1 += num2 % 10 ; NEW_LINE num2 /= 10 ; NEW_LINE DEDENT if ( sum1 <= K ) : NEW_LINE INDENT X = 0 ; NEW_LINE else : NEW_LINE DEDENT while ( temp > 0 ) : NEW_LINE var = int ( N // ( pow ( 10 , temp - 1 ) ) ) ; NEW_LINE INDENT temp_sum += var % 10 ; NEW_LINE if ( temp_sum >= K ) : NEW_LINE var = var // 10 ; NEW_LINE var += 1 ; NEW_LINE result = var * int ( pow ( 10 , temp ) ) ; NEW_LINE break ; NEW_LINE temp -= 1 ; NEW_LINE X = result - N ; NEW_LINE return X ; NEW_LINE DEDENT return - 1 ; NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 ; NEW_LINE K = 1 ; NEW_LINE DEDENT print ( minDigits ( N , K ) ) ; NEW_LINE