Open In App

CSES Solutions – Removing Digits

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

You are given an integer N. On each step, you may subtract one of the digits from the number. How many steps are required to make the number equal to 0?

Examples:

Input: N = 27
Output: 5
Explanation: 27 can be reduced to 0 in 5 steps:

  • Step 1: 27 – 7 = 20
  • Step 2: 20 – 2 = 18
  • Step 3: 18 – 8 = 10
  • Step 4: 10 – 1 = 9
  • Step 5: 9 – 9 = 0

Input: N = 17
Output: 3
Explanation: 17 can be reduced to 0 in 3 steps:

  • Step 1: 17 – 7 = 10
  • Step 2: 10 – 1 = 9
  • Step 3: 9 – 9 = 0

Approach: To solve the problem, follow the below idea:

The problem can be solved using Dynamic Programming. Maintain a dp[] array such that dp[i] stores the minimum number of steps to make i equal to 0. We can iterate i from 1 to N, and for each i, and minimize dp[i] using all the digits of i.

Let’s say we have already calculated minimum number of steps for all numbers from 1 to (i – 1), and now we need to calculate minimum steps for number i. To calculate minimum number of steps for number i, we will find all the digits of i and for every digit d, dp[i] = min(dp[i], 1 + dp[i – d]). After all the iterations, dp[N] will store the final answer.

Step-by-step algorithm:

  • Maintain a dp[] array such that dp[i] stores the minimum number of steps to reduce i to 0.
  • Initialize dp[0] = 0 as it takes 0 steps to reduce 0 to 0.
  • Iterate i for all numbers from 1 to N.
    • Find all the digits d of i.
    • For each digit d, update dp[i] = min(dp[i], 1 + dp[i – d]).
  • Return the final answer as dp[N].

Below is the implementation of the algorithm:

C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

// function to find the minimum number of steps to reduce N
// to 0
ll solve(ll N)
{
    // dp[] array such that dp[i] stores the minimum number
    // of steps to reduce i to 0
    vector<ll> dp(N + 1, 1e9);

    dp[0] = 0;

    // Iterate over all the numbers from 1 to N
    for (int i = 1; i <= N; i++) {
        ll temp = i;
        // Extract all the digits d and find the minimum of
        // dp[i - d]
        while (temp) {
            ll d = temp % 10;
            dp[i] = min(dp[i], 1 + dp[i - d]);
            temp /= 10;
        }
    }
    // Return dp[N] as the minimum number of steps to reduce
    // N to 0
    return dp[N];
}

int main()
{
    // Sample Input
    ll N = 17;

    cout << solve(N);
}
Java
import java.util.Arrays;

public class MinimumStepsToReduceToZero {

    // Function to find the minimum number of steps to reduce N to 0
    public static long solve(long N) {
        // dp[] array such that dp[i] stores the minimum number of steps to reduce i to 0
        long[] dp = new long[(int) (N + 1)];

        // Initialize dp[0] to 0 since it takes 0 steps to reduce 0 to 0
        dp[0] = 0;

        // Iterate over all the numbers from 1 to N
        for (int i = 1; i <= N; i++) {
            long temp = i;
            dp[i] = Integer.MAX_VALUE; // Initialize dp[i] to a large value

            // Extract all the digits d and find the minimum of dp[i - d]
            while (temp > 0) {
                long d = temp % 10; // Extract the last digit
                dp[i] = Math.min(dp[i], 1 + dp[(int) (i - d)]); // Update dp[i] with the minimum steps
                temp /= 10; // Remove the last digit
            }
        }

        // Return dp[N] as the minimum number of steps to reduce N to 0
        return dp[(int) N];
    }

    public static void main(String[] args) {
        // Sample Input
        long N = 17;
        System.out.println(solve(N)); // Output the minimum number of steps to reduce 17 to 0
    }
}
JavaScript
// Function to find the minimum number of steps to reduce N to 0
function solve(N) {
    // Array dp such that dp[i] stores the minimum number
    // of steps to reduce i to 0
    let dp = new Array(N + 1).fill(1e9);

    dp[0] = 0;

    // Iterate over all the numbers from 1 to N
    for (let i = 1; i <= N; i++) {
        let temp = i;
        // Extract all the digits d and find the minimum of
        // dp[i - d]
        while (temp) {
            let d = temp % 10;
            dp[i] = Math.min(dp[i], 1 + dp[i - d]);
            temp = Math.floor(temp / 10);
        }
    }
    // Return dp[N] as the minimum number of steps to reduce
    // N to 0
    return dp[N];
}

// Main function
function main() {
    // Sample Input
    let N = 17;

    console.log(solve(N)); // Output: 2
}

// Invoke the main function
main();
Python3
# Function to find the minimum number of steps to reduce N to 0
def solve(N):
    # dp[] array such that dp[i] stores the minimum number of steps to reduce i to 0
    dp = [float('inf')] * (N + 1)

    dp[0] = 0

    # Iterate over all the numbers from 1 to N
    for i in range(1, N + 1):
        temp = i
        # Extract all the digits d and find the minimum of dp[i - d]
        while temp:
            d = temp % 10
            dp[i] = min(dp[i], 1 + dp[i - d])
            temp //= 10

    # Return dp[N] as the minimum number of steps to reduce N to 0
    return dp[N]

# Sample Input
N = 17
print(solve(N))

Output
3



Time Complexity: O(N * logN), where N is the number which we need to reduce.
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads