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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
    // 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
    // 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 = 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
?>

chevron_right


Output:

Minimum possible digitsum is :1


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.