Min number of operations to reduce N to 0 by subtracting any digits from N | Function to reduce an integer N to Zero in minimum operations by removing digits from N ; Initialise dp [ ] to steps ; Iterate for all elements ; For each digit in number i ; Either select the number or do not select it ; dp [ N ] will give minimum step for N ; Given Number ; Function Call
function reduceZero ( N ) { var dp = Array ( N + 1 ) . fill ( 1000000000 ) ; dp [ 0 ] = 0 ; for ( var i = 0 ; i <= N ; i ++ ) { for ( var j = 0 ; j < i . toString ( ) . length ; j ++ ) { dp [ i ] = Math . min ( dp [ i ] , dp [ i - ( i . toString ( ) [ j ] - ' ' ) ] + 1 ) ; } } return dp [ N ] ; } var N = 25 ; document . write ( reduceZero ( N ) ) ;