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:
Please Login to comment...