Given an integer N, the task is to find the minimum count of numbers which needs to be added to get N. (All such numbers must have 9 as one’s digit)
Input: N = 27
27 = 9 + 9 + 9
Input: N = 109
109 itself has 9 as one’s digit.
- Check the one’s digit of N, based on one’s digit the minimum count of numbers which needs to be added can be easily found.
- If one’s digit is 9: The answer will 1 as the number itself has 9 as its one’s place digit.
- If one’s digit is:
- 1: 9 has to be added 9 times i.e. (9 * 9 = 81).
- 2: 9 has to be added 8 times i.e. (9 * 8 = 72).
- 3: 9 has to be added 7 times i.e. (9 * 7 = 63).
- 4: 9 has to be added 6 times i.e. (9 * 6 = 54).
- 5: 9 has to be added 5 times i.e. (9 * 5 = 45).
- 6: 9 has to be added 4 times i.e. (9 * 4 = 36).
- 7: 9 has to be added 3 times i.e. (9 * 3 = 27).
- 8: 9 has to be added 2 times i.e. (9 * 2 = 18).
- 0: 9 has to be added 10 times i.e. (9 * 10 = 90).
- Observation here is that only the minimum multiple count for all the cases above mentioned have to be added. This is because say for one’s digit as 4, all 9 (one’s place) from 6 numbers can be used and the result can be subtracted from N say it is M. Now, M will have 0 in one’s place. So, just use 5 numbers as 9 and sixth number as (M + 9).
Below is the implementation of the above approach:
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.
- Remove minimum numbers from the array to get minimum OR value
- Puzzle | Place numbers 1 to 9 in a Circle such that sum of every triplet in straight line is 15
- Minimum elements to be added in a range so that count of elements is divisible by K
- Minimum value to be added to X such that it is at least Y percent of N
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Minimum number to be added to all digits of X to make X > Y
- Find the minimum number to be added to N to make it a prime number
- Find the minimum number to be added to N to make it a power of K
- Minimum value of X that can be added to N to minimize sum of the digits to ≤ K
- Sum of first N natural numbers with all powers of 2 added twice
- Minimize sum of prime numbers added to make an array non-decreasing
- In-Place Algorithm
- Total number of ways to place X and Y at n places such that no two X are together
- Find the unit place digit of sum of N factorials
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Number of ways to place two queens on a N*N chess-board
- Place Value of a given digit in a number
- Count ways to place all the characters of two given strings alternately
- Find total no of collisions taking place between the balls in which initial direction of each ball is given