Count of minimum numbers having K as the last digit required to obtain sum N | C # 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
using System ; public 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 ] = Int32 . MaxValue ; MinimumSteps [ i ] = Int32 . MaxValue ; } 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 ] ; } } static public void Main ( ) { int N = 42 , K = 7 ; Console . Write ( minCount ( N , K ) ) ; } }