Smallest integer with digit sum M and multiple of N

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 [tabby title = "Python3"]

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 cotributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

8879


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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.