Given a number . Reduce this number to zero by subtracting the number by it’s most significant digit(Left most digit) at every step. The task is to count the number of steps it takes to be reduced to zero.

**Examples**:

Input:14Output:6Steps: 14 - 1 = 13 13 - 1 = 12 12 - 1 = 11 11 - 1 = 10 10 - 1 = 9 9 - 9 = 0Input:20Output:12 Numbers after series of steps: 20, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 0

**Naive Approach**: A naive approach is to reduce the number by its first digit step-wise and find the count of steps, but the time complexity will be huge if a large number is provided.

**Efficient Approach:** The main idea of the efficient approach is to reduce the number of steps in the naive approach. We can skip the steps whose leading digits are the same in consecutive numbers, and count them. The algorithm of skipping those numbers with the same leading digits is as follows:

- Let the number be last, count the digits in last and reduce it by 1, because the smallest number with same leading digit with the same count of digits will have that number of zeros in it.
- Find the first digit of the number of last, by last/count.
- Hence the smallest number of same number of count of digits with same leading number will be
**[first digit * (count-1)]** - the number of steps skipped can be achieved by
**[(last-smallest number)/first digit]**. - Hence the next number last will be
**last – (first*skipped)**

Below is the implementation of the above approach:

## C++

`// C++ program to find the count of Steps to ` `// reduce N to zero by subtracting its most ` `// significant digit at every step ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the number ` `// of digits in a number m ` `int` `countdig(` `int` `m) ` `{ ` ` ` `if` `(m == 0) ` ` ` `return` `0; ` ` ` `else` ` ` `return` `1 + countdig(m / 10); ` `} ` ` ` `// Function to count the number of ` `// steps to reach 0 ` `int` `countSteps(` `int` `x) ` `{ ` ` ` `// count the total number of stesp ` ` ` `int` `c = 0; ` ` ` `int` `last = x; ` ` ` ` ` `// iterate till we reach 0 ` ` ` `while` `(last) { ` ` ` ` ` `// count the digits in last ` ` ` `int` `digits = countdig(last); ` ` ` ` ` `// decrease it by 1 ` ` ` `digits -= 1; ` ` ` ` ` `// find the number on whose division, ` ` ` `// we get the first digit ` ` ` `int` `divisor = ` `pow` `(10, digits); ` ` ` ` ` `// first digit in last ` ` ` `int` `first = last / divisor; ` ` ` ` ` `// find the first number less than ` ` ` `// last where the first digit changes ` ` ` `int` `lastnumber = first * divisor; ` ` ` ` ` `// find the number of numbers ` ` ` `// with same first digit that are jumped ` ` ` `int` `skipped = (last - lastnumber) / first; ` ` ` ` ` `skipped += 1; ` ` ` ` ` `// count the steps ` ` ` `c += skipped; ` ` ` ` ` `// the next number with a different ` ` ` `// first digit ` ` ` `last = last - (first * skipped); ` ` ` `} ` ` ` ` ` `return` `c; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 14; ` ` ` ` ` `cout << countSteps(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the count of Steps to ` `// reduce N to zero by subtracting its most ` `// significant digit at every step ` ` ` ` ` `class` `GFG{ ` `// Function to count the number ` `// of digits in a number m ` `static` `int` `countdig(` `int` `m) ` `{ ` ` ` `if` `(m == ` `0` `) ` ` ` `return` `0` `; ` ` ` `else` ` ` `return` `1` `+ countdig(m / ` `10` `); ` `} ` ` ` `// Function to count the number of ` `// steps to reach 0 ` `static` `int` `countSteps(` `int` `x) ` `{ ` ` ` `// count the total number of stesp ` ` ` `int` `c = ` `0` `; ` ` ` `int` `last = x; ` ` ` ` ` `// iterate till we reach 0 ` ` ` `while` `(last>` `0` `) { ` ` ` ` ` `// count the digits in last ` ` ` `int` `digits = countdig(last); ` ` ` ` ` `// decrease it by 1 ` ` ` `digits -= ` `1` `; ` ` ` ` ` `// find the number on whose division, ` ` ` `// we get the first digit ` ` ` `int` `divisor = (` `int` `)Math.pow(` `10` `, digits); ` ` ` ` ` `// first digit in last ` ` ` `int` `first = last / divisor; ` ` ` ` ` `// find the first number less than ` ` ` `// last where the first digit changes ` ` ` `int` `lastnumber = first * divisor; ` ` ` ` ` `// find the number of numbers ` ` ` `// with same first digit that are jumped ` ` ` `int` `skipped = (last - lastnumber) / first; ` ` ` ` ` `skipped += ` `1` `; ` ` ` ` ` `// count the steps ` ` ` `c += skipped; ` ` ` ` ` `// the next number with a different ` ` ` `// first digit ` ` ` `last = last - (first * skipped); ` ` ` `} ` ` ` ` ` `return` `c; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `14` `; ` ` ` ` ` `System.out.println(countSteps(n)); ` `} ` `} ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find the ` `# count of Steps to reduce N to ` `# zero by subtracting its most ` `# significant digit at every step ` ` ` `# Function to count the number ` `# of digits in a number m ` `def` `countdig(m) : ` ` ` ` ` `if` `(m ` `=` `=` `0` `) : ` ` ` `return` `0` ` ` `else` `: ` ` ` `return` `1` `+` `countdig(m ` `/` `/` `10` `) ` ` ` `# Function to count the number ` `# of steps to reach 0 ` `def` `countSteps(x) : ` ` ` ` ` `# count the total number ` ` ` `# of stesp ` ` ` `c ` `=` `0` ` ` `last ` `=` `x ` ` ` ` ` `# iterate till we reach 0 ` ` ` `while` `(last) : ` ` ` ` ` `# count the digits in last ` ` ` `digits ` `=` `countdig(last) ` ` ` ` ` `# decrease it by 1 ` ` ` `digits ` `-` `=` `1` ` ` ` ` `# find the number on whose ` ` ` `# division, we get the first digit ` ` ` `divisor ` `=` `pow` `(` `10` `, digits) ` ` ` ` ` `# first digit in last ` ` ` `first ` `=` `last ` `/` `/` `divisor ` ` ` ` ` `# find the first number less ` ` ` `# than last where the first ` ` ` `# digit changes ` ` ` `lastnumber ` `=` `first ` `*` `divisor ` ` ` ` ` `# find the number of numbers ` ` ` `# with same first digit that ` ` ` `# are jumped ` ` ` `skipped ` `=` `(last ` `-` `lastnumber) ` `/` `/` `first ` ` ` ` ` `skipped ` `+` `=` `1` ` ` ` ` `# count the steps ` ` ` `c ` `+` `=` `skipped ` ` ` ` ` `# the next number with a different ` ` ` `# first digit ` ` ` `last ` `=` `last ` `-` `(first ` `*` `skipped) ` ` ` ` ` `return` `c ` ` ` `# Driver code ` `n ` `=` `14` `print` `(countSteps(n)) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the count of Steps to ` `// reduce N to zero by subtracting its most ` `// significant digit at every step ` `using` `System; ` ` ` `class` `GFG{ ` `// Function to count the number ` `// of digits in a number m ` `static` `int` `countdig(` `int` `m) ` `{ ` ` ` `if` `(m == 0) ` ` ` `return` `0; ` ` ` `else` ` ` `return` `1 + countdig(m / 10); ` `} ` ` ` `// Function to count the number of ` `// steps to reach 0 ` `static` `int` `countSteps(` `int` `x) ` `{ ` ` ` `// count the total number of stesp ` ` ` `int` `c = 0; ` ` ` `int` `last = x; ` ` ` ` ` `// iterate till we reach 0 ` ` ` `while` `(last>0) { ` ` ` ` ` `// count the digits in last ` ` ` `int` `digits = countdig(last); ` ` ` ` ` `// decrease it by 1 ` ` ` `digits -= 1; ` ` ` ` ` `// find the number on whose division, ` ` ` `// we get the first digit ` ` ` `int` `divisor = (` `int` `)Math.Pow(10, digits); ` ` ` ` ` `// first digit in last ` ` ` `int` `first = last / divisor; ` ` ` ` ` `// find the first number less than ` ` ` `// last where the first digit changes ` ` ` `int` `lastnumber = first * divisor; ` ` ` ` ` `// find the number of numbers ` ` ` `// with same first digit that are jumped ` ` ` `int` `skipped = (last - lastnumber) / first; ` ` ` ` ` `skipped += 1; ` ` ` ` ` `// count the steps ` ` ` `c += skipped; ` ` ` ` ` `// the next number with a different ` ` ` `// first digit ` ` ` `last = last - (first * skipped); ` ` ` `} ` ` ` ` ` `return` `c; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `n = 14; ` ` ` ` ` `Console.WriteLine(countSteps(n)); ` `} ` `} ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the count of Steps to ` `// reduce N to zero by subtracting its most ` `// significant digit at every step ` ` ` `// Function to count the number ` `// of digits in a number m ` `function` `countdig(` `$m` `) ` `{ ` ` ` `if` `(` `$m` `== 0) ` ` ` `return` `0; ` ` ` `else` ` ` `return` `1 + countdig( (int)(` `$m` `/ 10)); ` `} ` ` ` `// Function to count the number of ` `// steps to reach 0 ` `function` `countSteps(` `$x` `) ` `{ ` ` ` `// count the total number of stesp ` ` ` `$c` `= 0; ` ` ` `$last` `= ` `$x` `; ` ` ` ` ` `// iterate till we reach 0 ` ` ` `while` `(` `$last` `) ` ` ` `{ ` ` ` ` ` `// count the digits in last ` ` ` `$digits` `= countdig(` `$last` `); ` ` ` ` ` `// decrease it by 1 ` ` ` `$digits` `-= 1; ` ` ` ` ` `// find the number on whose division, ` ` ` `// we get the first digit ` ` ` `$divisor` `= pow(10, ` `$digits` `); ` ` ` ` ` `// first digit in last ` ` ` `$first` `= (int)(` `$last` `/ ` `$divisor` `); ` ` ` ` ` `// find the first number less than ` ` ` `// last where the first digit changes ` ` ` `$lastnumber` `= ` `$first` `* ` `$divisor` `; ` ` ` ` ` `// find the number of numbers ` ` ` `// with same first digit that are jumped ` ` ` `$skipped` `= (` `$last` `- ` `$lastnumber` `) / ` `$first` `; ` ` ` ` ` `$skipped` `+= 1; ` ` ` ` ` `// count the steps ` ` ` `$c` `+= ` `$skipped` `; ` ` ` ` ` `// the next number with a different ` ` ` `// first digit ` ` ` `$last` `= ` `$last` `- (` `$first` `* ` `$skipped` `); ` ` ` `} ` ` ` ` ` `return` `$c` `; ` `} ` ` ` `// Driver code ` `$n` `= 14; ` ` ` `echo` `countSteps(` `$n` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Minimum steps required to reduce all the elements of the array to zero
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Invert the Kth most significant bit of N
- Find most significant bit of a number X in base Y
- Right most non-zero digit in multiplication of array elements
- Program to make Indian Flag in Python | Step by Step
- Count number of step required to reduce N to 1 by following certain rule
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Number of steps to sort the array by changing order of three elements in each step
- Minimum Decrements on Subarrays required to reduce all Array elements to zero
- Count of N-digit numbers having digit XOR as single digit
- Minimum steps to reduce N to 0 by given operations
- Find the remainder when First digit of a number is divided by its Last digit
- Smallest N digit number with none of its digits as its divisor
- Reach A and B by multiplying them with K and K^2 at every step
- Round-off a number to a given number of significant digits
- Unset least significant K bits of a given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.