Count of minimum numbers having K as the last digit required to obtain sum N | Java program for 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 java . util . * ; class GFG { static int minCount ( int N , int K ) { int SmallestNumber [ ] = new int [ 10 ] ; int MinimumSteps [ ] = new int [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) { SmallestNumber [ i ] = Integer . MAX_VALUE ; MinimumSteps [ i ] = Integer . MAX_VALUE ; } for ( int i = 1 ; i <= 10 ; i ++ ) { int num = K * i ; SmallestNumber [ num % 10 ] = Math . min ( SmallestNumber [ num % 10 ] , num ) ; MinimumSteps [ num % 10 ] = Math . min ( MinimumSteps [ num % 10 ] , i ) ; } if ( N < SmallestNumber [ N % 10 ] ) { return - 1 ; } else { return MinimumSteps [ N % 10 ] ; } } public static void main ( String [ ] args ) { int N = 42 , K = 7 ; System . out . println ( minCount ( N , K ) ) ; } }