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++
#include <bits/stdc++.h>
using namespace std;
int digitSum( int n)
{
int ans = 0;
while (n) {
ans += n % 10;
n /= 10;
}
return ans;
}
int findInt( int n, int m)
{
int minDigit = floor (m / 9);
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;
}
int main()
{
int n = 13, m = 32;
cout << findInt(n, m);
return 0;
}
|
Java
class GFG
{
static int digitSum( int n)
{
int ans = 0 ;
while (n != 0 )
{
ans += n % 10 ;
n /= 10 ;
}
return ans;
}
static int findInt( int n, int m)
{
int minDigit = ( int )Math.floor(( double )(m / 9 ));
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 ;
}
static public void main(String args[])
{
int n = 13 , m = 32 ;
System.out.print(findInt(n, m));
}
}
|
Python3
from math import floor, pow
import sys
def digitSum(n):
ans = 0 ;
while (n):
ans + = n % 10 ;
n = int (n / 10 );
return ans
def findInt(n, m):
minDigit = floor(m / 9 )
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
if __name__ = = '__main__' :
n = 13
m = 32
print (findInt(n, m))
|
C#
using System;
class GFG
{
static int digitSum( int n)
{
int ans = 0;
while (n != 0)
{
ans += n % 10;
n /= 10;
}
return ans;
}
static int findInt( int n, int m)
{
int minDigit = ( int )Math.Floor(( double )(m / 9));
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;
}
static public void Main()
{
int n = 13, m = 32;
Console.WriteLine(findInt(n, m));
}
}
|
PHP
<?php
function digitSum( $n )
{
$ans = 0;
while ( $n )
{
$ans += $n % 10;
$n /= 10;
}
return $ans ;
}
function findInt( $n , $m )
{
$minDigit = floor ( $m / 9);
$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;
}
$n = 13;
$m = 32;
echo findInt( $n , $m );
# This code is contributed by ajit.
?>
|
Javascript
<script>
function digitSum(n)
{
var ans = 0;
while (n) {
ans += n % 10;
n = parseInt(n/10);
}
return ans;
}
function findInt(n, m)
{
var minDigit = Math.floor(m / 9);
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;
}
var n = 13, m = 32;
document.write( findInt(n, m));
</script>
|
Auxiliary Space: O(1)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...