Min number of operations to reduce N to 0 by subtracting any digits from N

Given a number N, the task is to find the minimum number of operations required to reduce the number N to zero by subtracting the given number by any digit present in it.
Examples:Â
Input: N = 4Â
Output: 1Â
Explanation:Â
Here 4 is the only digit present hence 4 – 4 = 0 and only one operation is required.Input: N = 17Â
Output: 3Â
Explanation:Â
The given integer is 17 and the steps of reduction are:Â
17 -> 17 – 7 = 10Â
10 -> 10 – 1 = 9Â
9 -> 9 – 9 = 0.Â
Hence 3 operations are required.Â
Approach: This problem can be solved using Dynamic Programming.Â
For any given number N, traverse each digit in N and recursively check by subtracting each digit one by one until N reduces to 0. But performing recursion will make the time complexity of the approach exponential.
Therefore, the idea is use an array(say dp[]) of size (N + 1) such that dp[i] will store the minimum number of operations needed to reduce i to 0.Â
For every digit x in the number N, the recurrence relation used is given by:Â Â
dp[i] = min(dp[i], dp[i-x] + 1),Â
where dp[i] will store the minimum number of operations needed to reduce i to 0.Â
Â
We will use Bottom-Up Approach to fill the array dp[] from 0 to N and then dp[N] will give the minimum number of operations for N.
Below is the implementation of the above approach:Â Â
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to reduce an integer N// to Zero in minimum operations by// removing digits from Nint reduceZero(int N){    // Initialise dp[] to steps    vector<int> dp(N + 1, 1e9);Â
    dp[0] = 0;Â
    // Iterate for all elements    for (int i = 0; i <= N; i++) {Â
        // For each digit in number i        for (char c : to_string(i)) {Â
            // Either select the number            // or do not select it            dp[i] = min(dp[i],                        dp[i - (c - '0')]                            + 1);        }    }Â
    // dp[N] will give minimum    // step for N    return dp[N];}Â
// Driver Codeint main(){    // Given Number    int N = 25;Â
    // Function Call    cout << reduceZero(N);    return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{Â
// Function to reduce an integer N// to Zero in minimum operations by// removing digits from Nstatic int reduceZero(int N){    // Initialise dp[] to steps    int []dp = new int[N + 1];    for (int i = 0; i <= N; i++)        dp[i] = (int) 1e9;    dp[0] = 0;Â
    // Iterate for all elements    for (int i = 0; i <= N; i++)     {Â
        // For each digit in number i        for (char c : String.valueOf(i).toCharArray())         {Â
            // Either select the number            // or do not select it            dp[i] = Math.min(dp[i],                              dp[i - (c - '0')] + 1);        }    }Â
    // dp[N] will give minimum    // step for N    return dp[N];}Â
// Driver Codepublic static void main(String[] args){    // Given Number    int N = 25;Â
    // Function Call    System.out.print(reduceZero(N));}}Â
// This code is contributed by amal kumar choubey |
Python3
# Python3 program for the above approachÂ
# Function to reduce an integer N# to Zero in minimum operations by# removing digits from Ndef reduceZero(N):         # Initialise dp[] to steps    dp = [1e9 for i in range(N + 1)]Â
    dp[0] = 0Â
    # Iterate for all elements    for i in range(N + 1):                 # For each digit in number i        for c in str(i):                         # Either select the number            # or do not select it            dp[i] = min(dp[i],                         dp[i - (ord(c) - 48)] + 1)Â
    # dp[N] will give minimum    # step for N    return dp[N]Â
# Driver CodeN = 25Â
# Function Callprint(reduceZero(N))Â
# This code is contributed by Sanjit_Prasad |
C#
// C# program for the above approachusing System;class GFG{Â
// Function to reduce an integer N// to Zero in minimum operations by// removing digits from Nstatic int reduceZero(int N){    // Initialise []dp to steps    int []dp = new int[N + 1];    for (int i = 0; i <= N; i++)        dp[i] = (int) 1e9;    dp[0] = 0;Â
    // Iterate for all elements    for (int i = 0; i <= N; i++)     {Â
        // For each digit in number i        foreach (char c in String.Join("", i).ToCharArray())         {Â
            // Either select the number            // or do not select it            dp[i] = Math.Min(dp[i],                              dp[i - (c - '0')] + 1);        }    }Â
    // dp[N] will give minimum    // step for N    return dp[N];}Â
// Driver Codepublic static void Main(String[] args){    // Given Number    int N = 25;Â
    // Function Call    Console.Write(reduceZero(N));}}Â
// This code is contributed by amal kumar choubey |
Javascript
<script>Â
// Javascript program for the above approachÂ
// Function to reduce an integer N// to Zero in minimum operations by// removing digits from Nfunction reduceZero(N){    // Initialise dp[] to steps    var dp = Array(N + 1).fill(1000000000);Â
    dp[0] = 0;Â
    // Iterate for all elements    for (var i = 0; i <= N; i++) {Â
        // For each digit in number i        for (var j =0; j< i.toString().length; j++)        {            // Either select the number            // or do not select it            dp[i] = Math.min(dp[i],                        dp[i - (i.toString()[j] - '0')]                            + 1);        }    }Â
    // dp[N] will give minimum    // step for N    return dp[N];}Â
// Driver CodeÂ
// Given Numbervar N = 25;Â
// Function Calldocument.write( reduceZero(N));Â
</script> |
5
Â
Time Complexity: O(N)Â
Auxiliary Space: O(N)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



