Open In App

Smallest integer with digit sum M and multiple of N

Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return digit sum
int digitSum(int n)
{
    int ans = 0;
    while (n) {
        ans += n % 10;
        n /= 10;
    }
 
    return ans;
}
 
// Function to find out the smallest integer
int 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 code
int main()
{
    int n = 13, m = 32;
    cout << findInt(n, m);
    return 0;
}


Java




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


Python3




# Python 3 implementation of the
# above approach
from math import floor, pow
 
import sys
 
# Function to return digit sum
def digitSum(n):
    ans = 0;
    while (n):
        ans += n % 10;
        n = int(n / 10);
 
    return ans
 
# Function to find out the smallest
# integer
def 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 code
if __name__ == '__main__':
    n = 13
    m = 32
    print(findInt(n, m))
 
# This code is contributed by
# Surendra_Gangwar


C#




// C# implementation of the above approach
using 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
//PHP implementation of the above approach
// Function to return digit sum
function digitSum($n)
{
    $ans = 0;
    while ($n)
    {
        $ans += $n % 10;
        $n /= 10;
    }
 
    return $ans;
}
 
// Function to find out the smallest integer
function 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.
?>


Javascript




<script>
 
 
// Javascript implementation of the above approach
 
// Function to return digit sum
function digitSum(n)
{
    var ans = 0;
    while (n) {
        ans += n % 10;
        n = parseInt(n/10);
    }
 
    return ans;
}
 
// Function to find out the smallest integer
function 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 code
var n = 13, m = 32;
document.write( findInt(n, m));
 
</script>


Output: 

8879

 

Auxiliary Space: O(1)



Last Updated : 23 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads