# Find minimum possible digit sum after adding a number d

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.

Examples:

```Input: n = 2546, d = 124
Output: 1
2546 + 8*124 = 3538
DigitSum(3538)=1

Input: n = 123, d = 3
Output: 3

```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. 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.
2. 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:

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function To find digitsum for a number ` `int` `digitsum(``int` `n) ` `{ ` `    ``// Logic for digitsum ` `    ``int` `r = n % 9; ` `    ``if` `(r == 0) ` `        ``return` `9; ` `    ``else` `        ``return` `r; ` `} ` ` `  `// Function to find minimum digit sum ` `int` `find(``int` `n, ``int` `d) ` `{ ` `    ``// Variable to store answer ` `    ``// Intialise by 10 as the answer ` `    ``// will always be less than 10 ` `    ``int` `minimum = 10; ` ` `  ` `  `    ``// Values of digitsum will repeat after ` `    ``// i=8, due to modulo taken with 9 ` `    ``for` `(``int` `i = 0; i < 9; i++) { ` `        ``int` `current = (n + i * d); ` `        ``minimum = min(minimum, digitsum(current)); ` `    ``} ` ` `  `    ``return` `minimum; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 2546, d = 124; ` `    ``cout << ``"Minimum possible digitsum is :"` `         ``<< find(n, d); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach ` `import` `java.io.*; ` `public` `class` `gfg ` `{ ` `    ``// Function To find digitsum for a number ` `public` `int` `digitsum(``int` `n) ` `{ ` `    ``// Logic for digitsum ` `    ``int` `r = n % ``9``; ` `    ``if` `(r == ``0``) ` `        ``return` `9``; ` `    ``else` `        ``return` `r; ` `} ` ` `  `// Function to find minimum digit sum ` `public` `int` `find(``int` `n, ``int` `d) ` `{ ` `    ``// Variable to store answer ` `    ``// Intialise by 10 as the answer ` `    ``// will always be less than 10 ` `    ``int` `minimum = ``10``; ` ` `  ` `  `    ``// Values of digitsum will repeat after ` `    ``// i=8, due to modulo taken with 9 ` `    ``for` `(``int` `i = ``0``; i < ``9``; i++) { ` `        ``int` `current = (n + i * d); ` `        ``minimum = Math.min(minimum, digitsum(current)); ` `    ``} ` ` `  `    ``return` `minimum; ` `} ` `} ` ` `  `class` `geek ` `{ ` `// Driver Code ` `public` `static` `void` `main(String[]args) ` `{ ` `    ``gfg g = ``new` `gfg(); ` `    ``int` `n = ``2546``, d = ``124``; ` `    ``System.out.println(``"Minimum possible digitsum is : "``+ (g.find(n, d))); ` `} ` `} ` `//This code is contributed by shs.. `

## Python3

 `# Python3 implementation of  ` `# above approach ` ` `  `# Function To find digitsum  ` `# for a number ` `def` `digitsum(n): ` ` `  `    ``# Logic for digitsum ` `    ``r ``=` `n ``%` `9``; ` `    ``if` `(r ``=``=` `0``): ` `        ``return` `9``; ` `    ``else``: ` `        ``return` `r; ` ` `  `# 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, ` `                      ``digitsum(current)); ` ` `  `    ``return` `minimum; ` ` `  `# Driver Code ` `n ``=` `2546``;  ` `d ``=` `124``; ` `print``(``"Minimum possible digitsum is :"``,  ` `                           ``find(n, d)); ` ` `  `# This code is contributed by mits `

## C#

 `// C# implementation of above approach ` `using` `System; ` `public` `class` `gfg ` `{ ` `    ``// Function To find digitsum for a number ` ` ``public` `int` `digitsum(``int` `n) ` ` ``{ ` `    ``// Logic for digitsum ` `    ``int` `r = n % 9; ` `    ``if` `(r == 0) ` `        ``return` `9; ` `    ``else` `        ``return` `r; ` ` ``} ` ` `  `// Function to find minimum digit sum ` ` ``public` `int` `find(``int` `n, ``int` `d) ` ` ``{ ` `    ``// Variable to store answer ` `    ``// Intialise by 10 as the answer ` `    ``// will always be less than 10 ` `    ``int` `minimum = 10; ` ` `  ` `  `    ``// Values of digitsum will repeat after ` `    ``// i=8, due to modulo taken with 9 ` `    ``for` `(``int` `i = 0; i < 9; i++) { ` `        ``int` `current = (n + i * d); ` `        ``minimum = Math.Min(minimum, digitsum(current)); ` `    ``} ` ` `  `    ``return` `minimum; ` ` ``} ` `} ` ` `  `class` `geek ` `{ ` `// Driver Code ` ` ``public` `static` `void` `Main() ` ` ``{ ` `    ``gfg g = ``new` `gfg(); ` `    ``int` `n = 2546, d = 124; ` `    ``Console.WriteLine(``"Minimum possible digitsum is : {0}"``, (g.find(n, d))); ` `    ``Console.Read(); ` ` ``} ` `} ` `//This code is contributed by SoumikMondal `

## PHP

 ` `

Output:

```Minimum possible digitsum is :1
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.