Min number of operations to reduce N to 0 by subtracting any digits from N
Last Updated :
02 Sep, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int reduceZero( int N)
{
vector< int > dp(N + 1, 1e9);
dp[0] = 0;
for ( int i = 0; i <= N; i++) {
for ( char c : to_string(i)) {
dp[i] = min(dp[i],
dp[i - (c - '0' )]
+ 1);
}
}
return dp[N];
}
int main()
{
int N = 25;
cout << reduceZero(N);
return 0;
}
|
Java
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));
}
}
|
Python3
def reduceZero(N):
dp = [ 1e9 for i in range (N + 1 )]
dp[ 0 ] = 0
for i in range (N + 1 ):
for c in str (i):
dp[i] = min (dp[i],
dp[i - ( ord (c) - 48 )] + 1 )
return dp[N]
N = 25
print (reduceZero(N))
|
C#
using System;
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++)
{
foreach ( char c in String.Join( "" , 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;
Console.Write(reduceZero(N));
}
}
|
Javascript
<script>
function reduceZero(N)
{
var dp = Array(N + 1).fill(1000000000);
dp[0] = 0;
for ( var i = 0; i <= N; i++) {
for ( var j =0; j< i.toString().length; j++)
{
dp[i] = Math.min(dp[i],
dp[i - (i.toString()[j] - '0' )]
+ 1);
}
}
return dp[N];
}
var N = 25;
document.write( reduceZero(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...