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.
Input: N = 4
Here 4 is the only digit present hence 4 – 4 = 0 and only one operation is required.
Input: N = 17
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:
Time Complexity: O(N)
Auxiliary Space: O(N)
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Reduce a number to 1 by performing given operations | Set 2
- Reduce a number to 1 by performing given operations
- Reduce N to 1 with minimum number of given operations
- Minimum number of operations required to reduce N to 0
- Minimum number of operations required to reduce N to 1
- Count the number of operations required to reduce the given number
- Min operations to reduce N by multiplying by any number or taking square root
- Minimum number of given operations required to reduce the array to 0 element
- Reduce the number to minimum multiple of 4 after removing the digits
- Bitwise Operations on Digits of a Number
- Find maximum operations to reduce N to 1
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Count operations of the given type required to reduce N to 0
- Pyramid form (increasing then decreasing) consecutive array using reduce operations
- Find smallest number with given number of digits and sum of digits under given constraints
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.