Given an integer N, the task is to find the minimum number of digits required to generate a number having the sum of digits equal to N.
Input: N = 18
The number with smallest number of digits having sum of digits equal to 18 is 99.
Input: N = 28
4-digit numbers like 8884, 6877, etc are the smallest in length having sum of digits equal to 28.
Approach: The problem can be solved by the following observations:
- Increment count by 9. Therefore, now count is equal to the number of 9’s in the shortest number. Reduce N to N % 9
- Now, if N exceeds 0, increment count by 1.
- Finally, print count as the answer.
Below is the implementation of above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum count of elements required to obtain the given Array by repeated mirror operations
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum number of operations required to obtain a given Binary String
- Minimum increments by index value required to obtain at least two equal Array elements
- Count of decrement operations required to obtain K in N steps
- Count of Ways to obtain given Sum from the given Array elements
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Count ways to obtain given sum by repeated throws of a dice
- Count of minimum reductions required to get the required sum K
- Minimize operations required to obtain N
- Minimize Steps required to obtain Sorted Order of an Array
- Minimum digits to be removed to make either all digits or alternating digits same
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum Steps to obtain N from 1 by the given operations
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Smallest number with given sum of digits and sum of square of digits
- Find K that requires minimum increments or decrements of array elements to obtain a sequence of increasing powers of K
- Find smallest number with given number of digits and sum of digits under given constraints
- Minimum and maximum number of digits required to be removed to make a given number divisible by 3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.