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 = 4Output:1Explanation:

Here 4 is the only digit present hence 4 – 4 = 0 and only one operation is required.Input:N = 17Output:3Explanation:

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 reducei 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 N ` `int` `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 Code ` `int` `main() ` `{ ` ` ` `// Given Number ` ` ` `int` `N = 25; ` ` ` ` ` `// Function Call ` ` ` `cout << reduceZero(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to reduce an integer N ` `// to Zero in minimum operations by ` `// removing digits from N ` `static` `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 Code ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to reduce an integer N ` `# to Zero in minimum operations by ` `# removing digits from N ` `def` `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 Code ` `N ` `=` `25` ` ` `# Function Call ` `print` `(reduceZero(N)) ` ` ` `# This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to reduce an integer N ` `// to Zero in minimum operations by ` `// removing digits from N ` `static` `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 Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `// Given Number ` ` ` `int` `N = 25; ` ` ` ` ` `// Function Call ` ` ` `Console.Write(reduceZero(N)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity:** *O(N)* **Auxiliary Space:** *O(N)*

## Recommended Posts:

- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Min operations to reduce N by multiplying by any number or taking square root
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Count the number of operations required to reduce the given number
- Reduce a number to 1 by performing given operations
- Minimum number of operations required to reduce N to 1
- Minimum number of given operations required to reduce the array to 0 element
- Reduce N to 1 with minimum number of given operations
- Reduce a number to 1 by performing given operations | Set 2
- Minimum number of operations required to reduce N to 0
- Reduce a given number to form a key by the given operations
- Reduce the number to minimum multiple of 4 after removing the digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Pyramid form (increasing then decreasing) consecutive array using reduce operations
- Count operations of the given type required to reduce N to 0
- Find maximum operations to reduce N to 1
- Minimize Cost to reduce the Array to a single element by given operations
- Minimum steps to reduce N to 0 by given operations
- Reduce N to 0 or less by given X and Y operations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.