Count of minimum numbers having K as the last digit required to obtain sum N | Python3 program for the above approach ; Stores the smallest number that ends with digit i ( 0 , 9 ) ; Stores the minimum number of steps to create a number ending with digit i ; Initialize elements as infinity ; Minimum number ending with digit i ; Minimum steps to create a number ending with digit i ; If N < SmallestNumber then , return - 1 ; Otherwise , return answer ; Driver Code
import sys NEW_LINE def minCount ( N , K ) : NEW_LINE INDENT SmallestNumber = [ 0 for i in range ( 10 ) ] NEW_LINE MinimumSteps = [ 0 for i in range ( 10 ) ] NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT SmallestNumber [ i ] = sys . maxsize ; NEW_LINE MinimumSteps [ i ] = sys . maxsize NEW_LINE DEDENT for i in range ( 1 , 11 , 1 ) : NEW_LINE INDENT num = K * i NEW_LINE SmallestNumber [ num % 10 ] = min ( SmallestNumber [ num % 10 ] , num ) NEW_LINE MinimumSteps [ num % 10 ] = min ( MinimumSteps [ num % 10 ] , i ) NEW_LINE DEDENT if ( N < SmallestNumber [ N % 10 ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return MinimumSteps [ N % 10 ] NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 42 NEW_LINE K = 7 NEW_LINE print ( minCount ( N , K ) ) NEW_LINE DEDENT