Find minimum possible digit sum after adding a number d Last Updated : 20 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Approach: 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: C++ // C++ implementation of above approach #include <bits/stdc++.h> 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 // Initialise 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 // Initialise 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 # Initialise 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 // Initialise 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 <?php // PHP implementation of // above approach // Function To find digitsum // for a number function digitsum($n) { // Logic for digitsum $r = $n % 9; if ($r == 0) return 9; else return $r; } // Function to find minimum digit sum function find($n, $d) { // Variable to store answer // Initialise 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 = 0; $i < 9; $i++) { $current = ($n + $i * $d); $minimum = min($minimum, digitsum($current)); } return $minimum; } // Driver Code $n = 2546; $d = 124; echo "Minimum possible digitsum is :", find($n, $d); // This code is contributed // by Shashank ?> JavaScript <script> // javascript implementation of above approach // Function To find digitsum for a number function digitsum( n) { // Logic for digitsum var r = n % 9; if (r == 0) return 9; else return r; } // Function to find minimum digit sum function find( n, d) { // Variable to store answer // Initialise by 10 as the answer // will always be less than 10 var minimum = 10; // Values of digitsum will repeat after // i=8, due to modulo taken with 9 for (var i = 0; i < 9; i++) { var current = (n + i * d); minimum = Math.min(minimum, digitsum(current)); } return minimum; } var n = 2546, d = 124; document.write("Minimum possible digitsum is :" + find(n, d)); </script> Output: Minimum possible digitsum is :1 Time Complexity: O(9) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Finding sum of digits of a number until sum becomes single digit K krikti Follow Improve Article Tags : Misc Mathematical DSA number-digits number-theory +1 More Practice Tags : MathematicalMiscnumber-theory Similar Reads Minimum number with digits as 4 and 7 only and given sum Lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. What minimum lucky number has the sum of digits equal to n. Examples: Input : sum = 11 Output : 47 Sum of digits in 47 is 11 and 47 is the smallest number with given sum. Input : sum = 10 Out 6 min read Find second smallest number from sum of digits and number of digits Given the sum of digits as S and the number of digits as D, the task is to find the second smallest number Examples: Input: S = 9, D = 2Output: 27Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Input: S = 16, D = 3Output: 178Expla 8 min read Finding sum of digits of a number until sum becomes single digit Given an integer n, we need to repeatedly find the sum of its digits until the result becomes a single-digit number.Examples:Input: n = 1234 Output: 1 Explanation:Step 1: 1 + 2 + 3 + 4 = 10 Step 2: 1 + 0 = 1Input: n = 5674Output: 4Explanation: Step 1: 5 + 6 + 7 + 4 = 22 Step 2: 2 + 2 = 4Table of Con 5 min read Minimum Possible sum of digits in a positive multiple of N Given a number N, find the minimum possible sum of digits that can be obtained from a positive Multiple of N. Constraints : 1<=N<=10^5. Examples: Input : N = 6 Output : 3 Explanation: 6*2 = 12, sum of digits is 1+2 = 3. Input : N = 20 Output : 1 20*5 = 100, sum of digits is 1+0+0=1 Approach : 10 min read Count all possible N digit numbers that satisfy the given condition Given an integer N, the task is to count all possible N digit numbers such that A + reverse(A) = 10N - 1 where A is an N digit number and reverse(A) is reverse of A. A shouldn't have any leading 0s.Examples: Input: N = 2 Output: 9 All possible 2 digit numbers are 90, 81, 72, 63, 54, 45, 36, 27 and 1 4 min read Compute sum of digits in all numbers from 1 to n Given a number n, the task is to find the sum of digits in all numbers from 1 to n. Examples: Input: n = 5Output: 15Explanation: Sum of digits in numbers from 1 to 5 = 15Input: n = 12Output: 51Explanation: Sum of digits in numbers from 1 to 12 = 51Table of Content[Naive Approach] By Traversing Every 15+ min read Like