Smallest integer with digit sum M and multiple of N

23 Jun, 2022
Given two positive integers N and M, the task is to find the smallest positive integer which is divisible by N and whose digit sum is M. Print -1 if no such integer exists within the range of int.
Examples: 
Input: N = 13, M = 32
Output: 8879
8879 is divisible by 13 and its 
Sum of digits of 8879 is 8+8+7+9 = 32 
i.e. equals to M

Input: N = 8, M = 32;
Output: 8888
Approach: Start with N and iterate over all the multiples of n, and check whether its digit sum is equal to m or not. This problem can be solved in logn(INT_MAX) * log10(INT_MAX) time. The efficiency of this approach can be increased by starting the iteration with a number which has at least m/9 digit. 
Below is the implementation of the approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; // Function to return digit sumint digitSum(int n){    int ans = 0;    while (n) {        ans += n % 10;        n /= 10;    }     return ans;} // Function to find out the smallest integerint findInt(int n, int m){    int minDigit = floor(m / 9);     // Start of the iterator (Smallest multiple of n)    int start = pow(10, minDigit) -                (int)pow(10, minDigit) % n;     while (start < INT_MAX) {        if (digitSum(start) == m)            return start;        else            start += n;    }    return -1;} // Driver codeint main(){    int n = 13, m = 32;    cout << findInt(n, m);    return 0;}
// Java implementation of the above approach class GFG{     // Function to return digit sum    static int digitSum(int n)    {        int ans = 0;        while (n != 0)        {            ans += n % 10;            n /= 10;        }             return ans;    }         // Function to find out the    // smallest integer    static int findInt(int n, int m)    {        int minDigit = (int)Math.floor((double)(m / 9));             // Start of the iterator (Smallest multiple of n)        int start = (int)Math.pow(10, minDigit) -                    (int)Math.pow(10, minDigit) % n;             while (start < Integer.MAX_VALUE)        {            if (digitSum(start) == m)                return start;            else                start += n;        }        return -1;    }     // Driver code    static public void main(String args[])    {        int n = 13, m = 32;        System.out.print(findInt(n, m));    }} // This code is contributed// by Akanksha Rai
# Python 3 implementation of the# above approachfrom math import floor, pow import sys # Function to return digit sumdef digitSum(n):    ans = 0;    while (n):        ans += n % 10;        n = int(n / 10);     return ans # Function to find out the smallest# integerdef findInt(n, m):    minDigit = floor(m / 9)     # Start of the iterator (Smallest    # multiple of n)    start = (int(pow(10, minDigit)) -             int(pow(10, minDigit)) % n)     while (start < sys.maxsize):        if (digitSum(start) == m):            return start        else:            start += n    return -1 # Driver codeif __name__ == '__main__':    n = 13    m = 32    print(findInt(n, m)) # This code is contributed by# Surendra_Gangwar
// C# implementation of the above approachusing System; class GFG{     // Function to return digit sum    static int digitSum(int n)    {        int ans = 0;        while (n != 0)        {            ans += n % 10;            n /= 10;        }             return ans;    }         // Function to find out the    // smallest integer    static int findInt(int n, int m)    {        int minDigit = (int)Math.Floor((double)(m / 9));             // Start of the iterator (Smallest multiple of n)        int start = (int)Math.Pow(10, minDigit) -                    (int)Math.Pow(10, minDigit) % n;             while (start < int.MaxValue)        {            if (digitSum(start) == m)                return start;            else                start += n;        }        return -1;    }     // Driver code    static public void Main()    {        int n = 13, m = 32;        Console.WriteLine(findInt(n, m));    }} // This code is contributed by Ryuga
<?php//PHP implementation of the above approach// Function to return digit sumfunction digitSum($n){    $ans = 0;    while ($n)    {        $ans += $n % 10;        $n /= 10;    }     return $ans;} // Function to find out the smallest integerfunction findInt($n, $m){    $minDigit = floor($m / 9);     // Start of the iterator (Smallest multiple of n)    $start = pow(10, $minDigit) -                (int)pow(10, $minDigit) % $n;     while ($start < PHP_INT_MAX)    {        if (digitSum($start) == $m)            return $start;        else            $start += $n;    }    return -1;}     // Driver code    $n = 13;    $m = 32;    echo findInt($n, $m); # This code is contributed by ajit.?>
<script>  // Javascript implementation of the above approach // Function to return digit sumfunction digitSum(n){    var ans = 0;    while (n) {        ans += n % 10;        n = parseInt(n/10);    }     return ans;} // Function to find out the smallest integerfunction findInt(n, m){    var minDigit = Math.floor(m / 9);     // Start of the iterator (Smallest multiple of n)    var start = Math.pow(10, minDigit) -                Math.pow(10, minDigit) % n;     while (start < 1000000000) {        if (digitSum(start) == m)            return start;        else            start += n;    }    return -1;} // Driver codevar n = 13, m = 32;document.write( findInt(n, m)); </script>
8879
 
Auxiliary Space: O(1)
jit_t
ankthon
SURENDRA_GANGWAR
Akanksha_Rai
itsok
simmytarika5
rishavmahato348
divisors
number-digits
Competitive Programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count of strings whose prefix match with the given string to a given length k
Most important type of Algorithms
The Ultimate Beginner's Guide For DSA
Find two numbers from their sum and XOR
C++: Methods of code shortening in competitive programming
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays