Given two numbers and N and D. Apply any of two below operations to N:
- add D to N
- change N to digitsum(N), where digitsum(N) is the sum of digits of N
The task is to transform N to the minimum possible value. Print the minimum possible value of N and the number of times the given operations applied(any one of them). The number of operations must be minimum.
Input : N = 2, D = 1
Output : 1 9
Perfome Type1 opeation 8 times and Type2 opeation 1 time
Input : N = 9, D = 3
Output : 3, 2
Apply one type1 operation first and then type2 operation
Let Dr(x) be a function defined for integer x as :
Dr(x) = x, if
0 <= x <= 9
Dr(x) = Dr(Sum-of-digits(x))
The function Dr(x) is the digital root of a number x.
Dr(a+b) = Dr(Dr(a) + Dr(b))
Dr(ab) = Dr(Dr(a) * Dr(b))
Important observation : The minimum value is always the minimum over :
Dr(N + kD) for some non-negative integer k.
Dr(N + kD) = Dr(Dr(N) + Dr(kD)) (1)
Dr(kd) = Dr(Dr(k) * Dr(D))
Possible values of Dr(k) are 0, 1, 2…9, given by numbers k=0, 1, 2…9
Dr(x) = Dr(Sum-of-digits(x)) (2)
- The minimum value for N is equal to the minimum value for Sum-of-digits(N). If we reduce this answer once and add D, the minimum value that can be obtained wouldn’t change. So, if it is required to perform a reduce operation and then an add operation, then we can do the add operation and then the reduce operation without affecting the possible roots we can reach. This is evident from combination of formulae (1) and (2)
So, we can do all add operations first, all reduce operations later, and reach any number that can be possibly reached by any set of operations. Using the above claims, we can prove the minimum possible value is the minimum of
Dr(N + kD)where
0 <= k <= 9.
- To find the minimum number of steps, note that the relative order of the add and Sum-of-digits operations does affect the answer. Also, note that the Sum-of-digits function is an decreases extremely fast.
- Any number <= 1010 goes to a number <= 90, any number <= 90 goes to something <= 18 and so on. In short, any number can be reduced to its digital root in <= 5 steps.
- Via this, we can prove that the value of the minimum steps can never be greater than 15. This is a loose upper bound, not the exact one.
Use brute force recursion algorithm, that at each step branches in 2 different directions, one
x = Sum-of-digits(x), the other being
x = x+D, but only until a recursion depth of 15. In this way, we stop after exploring 215 different ways.
Below is the implementation of the above approach:
Time Complexity :
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 possible value T such that at most D Partitions of the Array having at most sum T is possible
- Find the maximum possible value of the minimum value of modified array
- Minimum LCM and GCD possible among all possible sub-arrays
- Discrete Cosine Transform (Algorithm and Program)
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Minimum value possible of a given function from the given set
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Minimum possible value of (i * j) % 2019
- Minimum possible value of max(A, B) such that LCM(A, B) = C
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Remove minimum numbers from the array to get minimum OR value
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Find the maximum possible value of last element of the Array
- Find the maximum possible value for the given periodic function
- Possible pairs forming a Pythagorean Triple with a given value
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Find the largest possible value of K such that K modulo X is Y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.