Given a number n and a number d, we can add d to n as many times ( even 0 is possible ). The task is to find the minimum possible digit sum we can achieve by performing above operation.
Digit Sum is defined as the sum of digits of a number recursively until it is less than 10.
Input: n = 2546, d = 124 Output: 1 2546 + 8*124 = 3538 DigitSum(3538)=1 Input: n = 123, d = 3 Output: 3
- First observation here is to use %9 approach to find minimum possible digit sum of a number n. If modulo with 9 is 0 return 9 else return the remainder.
- Second observation is, a+d*(9k+l) modulo 9 is equivalent to a+d*l modulo 9, therefore, the answer to the query will be available in either no addition or first 8 additions of d, after which the digit sum will repeat.
Below is the implementation of above approach:
# Python3 implementation of
# above approach
# Function To find digitsum
# for a number
# Logic for digitsum
r = n % 9;
if (r == 0):
# Function to find minimum digit sum
def find(n, d):
# Variable to store answer
# Intialise by 10 as the answer
# will always be less than 10
minimum = 10;
# Values of digitsum will
# repeat after i=8, due to
# modulo taken with 9
for i in range(9):
current = (n + i * d);
minimum = min(minimum,
# Driver Code
n = 2546;
d = 124;
print(“Minimum possible digitsum is :”,
# This code is contributed by mits
Minimum possible digitsum is :1
- Find the Number which contain the digit d
- Find nth number that contains the digit k or divisible by k.
- Program to find last digit of n'th Fibonnaci Number
- Find last five digits of a given five digit number raised to power five
- Count of Numbers in Range where first digit is equal to last digit of the number
- Largest number less than N with digit sum greater than the digit sum of N
- Find minimum number to be divided to make a number a perfect square
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Find minimum sum of factors of number
- Find the minimum number of steps to reach M from N
- Find minimum number of coins that make a given value
- Number of positions such that adding K to the element is greater than sum of all other elements
- Check whether the number can be made perfect square after adding 1
- Find the minimum number of rectangles left after inserting one into another
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.