Min number of operations to reduce N to 0 by subtracting any digits from N | Java program for the above approach ; 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 ; Driver Code ; Given Number ; Function Call
import java . util . * ; class GFG { static int reduceZero ( int N ) { int [ ] dp = new int [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) dp [ i ] = ( int ) 1e9 ; dp [ 0 ] = 0 ; for ( int i = 0 ; i <= N ; i ++ ) { for ( char c : String . valueOf ( i ) . toCharArray ( ) ) { dp [ i ] = Math . min ( dp [ i ] , dp [ i - ( c - '0' ) ] + 1 ) ; } } return dp [ N ] ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . print ( reduceZero ( N ) ) ; } }