Largest number N which can be reduced to 0 in K steps
Given an integer N. The following tasks are performed:
- The number is noted.
- The leading digit from N is subtracted from N and the resulting value is stored back in N.
- The new value of N is again noted.
- This process is continued till N becomes 0. Finally, 0 is noted down in the end.
For example, take N = 13. The numbers noted down in the process of reducing 13 to 0 will be:
- 13
- 13 – 1 = 12
- 12 – 1 = 11
- 11 – 1 = 10
- 10 – 1 = 9
- 9 – 9 = 0
Given an integer K, which is the total count of numbers noted down in the process of reducing a number N to 0 as described above, the task is to find the largest possible value of the starting number N.
Examples:
Input: k = 2
Output: 9
Explanation:
- Numbers noted = { 9 }
- Subtract Leading digit: 9 – 9 = 0. Numbers noted = { 9, 0 }
Input: k = 3
Output: 10
Explanation:
- Numbers noted = { 10 }
- Subtract Leading digit: 10 – 1 = 9. Numbers noted = { 10, 9 }
- Subtract Leading digit: 9 – 9 = 0. Numbers noted = { 10, 9, 0 }
Approach: The idea is to observe that the maximum possible value of N will always be less than 10*K. Therefore, the idea is to apply binary search in the range K to 10*K and to look for the greatest number which can be reduced to 0 in K steps.
To find the largest possible number:
- Initialize left to k and right to k*10.
- Initialize mid to ( left + right ) / 2.
- Get the count of the number noted down to convert mid to 0 and store it in len.
-
Follow divide and conquer approach: While len is not equal to k:
- Update the mid to the current ( left + right ) / 2.
- Get the count when starting with mid.
- If the count is greater than mid, update right to mid.
- Else, update left to mid.
- Now, mid has one value which will result in k integers written down.
-
To find the largest such number:
- While the count is equal to k:
- If current count is not equal to count got by taking mid+1, break
- Else, keep incrementing mid by 1.
- mid is now the largest possible integer if k integers are written down.
Below is the implementation of the above approach:
C++
// C++ program to implement above approach #include <bits/stdc++.h> using namespace std; // Utility function to return the first // digit of a number. int firstDigit(int n) { // Remove last digit from number // till only one digit is left while (n >= 10) { n /= 10; } // return the first digit return n; } // Utility function that returns the count of numbers // written down when starting from n int getCount(int n) { int count = 1; while (n != 0) { int leadDigit = firstDigit(n); n -= leadDigit; count++; } return count; } // Function to find the largest number N which // can be reduced to 0 in K steps int getLargestNumber(int k) { int left = k; int right = k * 10; int mid = (left + right) / 2; // Get the sequence length of the mid point int len = getCount(mid); // Until k sequence length is reached while (len != k) { // Update mid point mid = (left + right) / 2; // Get count of the new mid point len = getCount(mid); if (len > k) { // Update right to mid right = mid; } else { // Update left to mid left = mid; } } // Increment mid point by one while count // is equal to k to get the maximum value // of mid point while (len == k) { if (len != getCount(mid + 1)) { break; } mid++; } return (mid); } // Driver Code int main() { int k = 3; cout << getLargestNumber(k); return 0; } |
Java
// Java program to implement above approach import java.io.*; class GFG { // Utility function to return the first // digit of a number. static int firstDigit(int n) { // Remove last digit from number // till only one digit is left while (n >= 10) { n /= 10; } // return the first digit return n; } // Utility function that returns the count of numbers // written down when starting from n static int getCount(int n) { int count = 1; while (n != 0) { int leadDigit = firstDigit(n); n -= leadDigit; count++; } return count; } // Function to find the largest number N which // can be reduced to 0 in K steps static int getLargestNumber(int k) { int left = k; int right = k * 10; int mid = (left + right) / 2; // Get the sequence length of the mid point int len = getCount(mid); // Until k sequence length is reached while (len != k) { // Update mid point mid = (left + right) / 2; // Get count of the new mid point len = getCount(mid); if (len > k) { // Update right to mid right = mid; } else { // Update left to mid left = mid; } } // Increment mid point by one while count // is equal to k to get the maximum value // of mid point while (len == k) { if (len != getCount(mid + 1)) { break; } mid++; } return (mid); } // Driver Code public static void main (String[] args) { int k = 3; System.out.println (getLargestNumber(k)); } } // This code is contributed by jit_t |
Python3
# Python3 program to implement above approach # Utility function to return the first # digit of a number. def firstDigit(n) : # Remove last digit from number # till only one digit is left while (n >= 10) : n //= 10; # return the first digit return n; # Utility function that returns # the count of numbers written # down when starting from n def getCount(n) : count = 1; while (n != 0) : leadDigit = firstDigit(n); n -= leadDigit; count += 1; return count; # Function to find the largest number N # which can be reduced to 0 in K steps def getLargestNumber(k) : left = k; right = k * 10; mid = (left + right) // 2; # Get the sequence length of the mid point length = getCount(mid); # Until k sequence length is reached while (length != k) : # Update mid point mid = (left + right) // 2; # Get count of the new mid point length = getCount(mid); if (length > k) : # Update right to mid right = mid; else : # Update left to mid left = mid; # Increment mid point by one while count # is equal to k to get the maximum value # of mid point while (length == k) : if (length != getCount(mid + 1)) : break; mid += 1; return mid; # Driver Code if __name__ == "__main__" : k = 3; print(getLargestNumber(k)); # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System; class GFG { // Utility function to return the first // digit of a number. static int firstDigit(int n) { // Remove last digit from number // till only one digit is left while (n >= 10) { n /= 10; } // return the first digit return n; } // Utility function that returns the count of numbers // written down when starting from n static int getCount(int n) { int count = 1; while (n != 0) { int leadDigit = firstDigit(n); n -= leadDigit; count++; } return count; } // Function to find the largest number N which // can be reduced to 0 in K steps static int getLargestNumber(int k) { int left = k; int right = k * 10; int mid = (left + right) / 2; // Get the sequence length of the mid point int len = getCount(mid); // Until k sequence length is reached while (len != k) { // Update mid point mid = (left + right) / 2; // Get count of the new mid point len = getCount(mid); if (len > k) { // Update right to mid right = mid; } else { // Update left to mid left = mid; } } // Increment mid point by one while count // is equal to k to get the maximum value // of mid point while (len == k) { if (len != getCount(mid + 1)) { break; } mid++; } return (mid); } // Driver Code public static void Main(String []args) { int k = 3; Console.WriteLine( getLargestNumber(k)); } } // This code is contributed by Arnab Kundu |
PHP
<?php // PHP program to implement above approach // Utility function to return the // first digit of a number. function firstDigit($n) { // Remove last digit from number // till only one digit is left while ($n >= 10) { $n = (int)($n / 10); } // return the first digit return $n; } // Utility function that returns the // count of numbers written down when // starting from n function getCount($n) { $count = 1; while ($n != 0) { $leadDigit = firstDigit($n); $n -= $leadDigit; $count++; } return $count; } // Function to find the largest number N // which can be reduced to 0 in K steps function getLargestNumber($k) { $left = $k; $right = $k * 10; $mid = (int)(($left + $right) / 2); // Get the sequence length // of the mid point $len = getCount($mid); // Until k sequence length is reached while ($len != $k) { // Update mid point $mid = (int)(($left + $right) / 2); // Get count of the new mid point $len = getCount($mid); if ($len > $k) { // Update right to mid $right = $mid; } else { // Update left to mid $left = $mid; } } // Increment mid point by one while count // is equal to k to get the maximum value // of mid point while ($len == $k) { if ($len != getCount($mid + 1)) { break; } $mid++; } return ($mid); } // Driver Code $k = 3; echo(getLargestNumber($k)); // This code is contributed by Code_Mech. ?> |
10
Recommended Posts:
- Maximum of sum and product of digits until number is reduced to a single digit
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Number of steps required to convert a binary number to one
- Find the number of stair steps
- Collect all coins in minimum number of steps
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Print steps to make a number in form of 2^X - 1
- Number of steps to convert to prime factors
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Find the minimum number of steps to reach M from N
- Minimize the number of steps required to reach the end of the array | Set 2
- Product of given N fractions in reduced form
- Find the index which is the last to be reduced to zero after performing a given operation
- Number of times the largest perfect square number can be subtracted from N
- Find the largest good number in the divisors of given number N
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.

