CSES Solutions - Removing Digits
Last Updated :
28 Mar, 2024
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))
Time Complexity: O(N * logN), where N is the number which we need to reduce.
Auxiliary Space: O(N)
Similar Reads
Lowest Number by Removing k digits Given a string s consisting of digits and an integer k, remove k digits from the string to form the smallest possible number, maintaining the order of the remaining digits. The resulting number should not have leading zeros. if the number is empty after removing digits, return "0".Examples: Input: s
13 min read
Removing elements between the two zeros Given an integer N which shows the size of the string and in the next line given a string which contains a string of character with only zero and one. The task is to remove a single character each time that comes in between the two zero characters.During each turn, only one character from the string
4 min read
Move all digits to the beginning of a given string Given a string S, the task is to move all the digits present in the string, to the beginning of the string. Examples: Input: S = âGeeks4forGeeks123âOutput: 4123GeeksforGeeksExplanation:The given string contains digits 4, 1, 2, and 3. Moving all the digits to the beginning of the string modifies the
7 min read
Different Ways to Remove all the Digits from String in Java Given alphanumeric string str, the task is to write a Java program to remove all the digit from this string and prints the modified string. Examples: Input: str = âGeeksForGeeks123âOutput: GeeksForGeeksExplanation: The given string contains digits 1, 2, and 3. We remove all the digit and prints the
3 min read
Sum of Digits of a Number Given a number n, find the sum of its digits.Examples : Input: n = 687Output: 21Explanation: The sum of its digits are: 6 + 8 + 7 = 21Input: n = 12Output: 3Explanation: The sum of its digits are: 1 + 2 = 3Table of Content[Approach 1] Digit Extraction - O(log10n) Time and O(1) Space[Approach 2] Using
6 min read