Find smallest number with given number of digits and sum of digits
Last Updated :
25 May, 2022
How to find the smallest number with given digit sum s and number of digits d?
Examples :
Input : s = 9, d = 2
Output : 18
There are many other possible numbers
like 45, 54, 90, etc with sum of digits
as 9 and number of digits as 2. The
smallest of them is 18.
Input : s = 20, d = 3
Output : 299
A Simple Solution is to consider all m digit numbers and keep track of minimum number with digit sum as s. A close upper bound on time complexity of this solution is O(10m).
There is a Greedy approach to solve the problem. The idea is to one by one fill all digits from rightmost to leftmost (or from least significant digit to most significant).
We initially deduct 1 from sum s so that we have smallest digit at the end. After deducting 1, we apply greedy approach. We compare remaining sum with 9, if remaining sum is more than 9, we put 9 at the current position, else we put the remaining sum. Since we fill digits from right to left, we put the highest digits on the right side. Below is implementation of the idea.
C++
#include <iostream>
using namespace std;
void findSmallest( int m, int s)
{
if (s == 0)
{
(m == 1)? cout << "Smallest number is " << 0
: cout << "Not possible" ;
return ;
}
if (s > 9*m)
{
cout << "Not possible" ;
return ;
}
int res[m];
s -= 1;
for ( int i=m-1; i>0; i--)
{
if (s > 9)
{
res[i] = 9;
s -= 9;
}
else
{
res[i] = s;
s = 0;
}
}
res[0] = s + 1;
cout << "Smallest number is " ;
for ( int i=0; i<m; i++)
cout << res[i];
}
int main()
{
int s = 9, m = 2;
findSmallest(m, s);
return 0;
}
|
Java
class GFG
{
static void findSmallest( int m, int s)
{
if (s == 0 )
{
System.out.print(m == 1 ? "Smallest number is 0" : "Not possible" );
return ;
}
if (s > 9 *m)
{
System.out.println( "Not possible" );
return ;
}
int [] res = new int [m];
s -= 1 ;
for ( int i=m- 1 ; i> 0 ; i--)
{
if (s > 9 )
{
res[i] = 9 ;
s -= 9 ;
}
else
{
res[i] = s;
s = 0 ;
}
}
res[ 0 ] = s + 1 ;
System.out.print( "Smallest number is " );
for ( int i= 0 ; i<m; i++)
System.out.print(res[i]);
}
public static void main (String[] args)
{
int s = 9 , m = 2 ;
findSmallest(m, s);
}
}
|
Python3
def findSmallest(m,s):
if (s = = 0 ):
if (m = = 1 ) :
print ( "Smallest number is 0" )
else :
print ( "Not possible" )
return
if (s > 9 * m):
print ( "Not possible" )
return
res = [ 0 for i in range (m + 1 )]
s - = 1
for i in range (m - 1 , 0 , - 1 ):
if (s > 9 ):
res[i] = 9
s - = 9
else :
res[i] = s
s = 0
res[ 0 ] = s + 1
print ( "Smallest number is " ,end = "")
for i in range (m):
print (res[i],end = "")
s = 9
m = 2
findSmallest(m, s)
|
C#
using System;
class GFG
{
static void findSmallest( int m, int s)
{
if (s == 0)
{
Console.Write(m == 1 ?
"Smallest number is 0" :
"Not possible" );
return ;
}
if (s > 9 * m)
{
Console.Write( "Not possible" );
return ;
}
int []res = new int [m];
s -= 1;
for ( int i = m - 1; i > 0; i--)
{
if (s > 9)
{
res[i] = 9;
s -= 9;
}
else
{
res[i] = s;
s = 0;
}
}
res[0] = s + 1;
Console.Write( "Smallest number is " );
for ( int i = 0; i < m; i++)
Console.Write(res[i]);
}
public static void Main ()
{
int s = 9, m = 2;
findSmallest(m, s);
}
}
|
PHP
<?php
function findSmallest( $m , $s )
{
if ( $s == 0)
{
if (( $m == 1) == true)
echo "Smallest number is " , 0;
else
echo "Not possible" ;
return ;
}
if ( $s > 9 * $m )
{
echo "Not possible" ;
return ;
}
$s -= 1;
for ( $i = $m - 1; $i > 0; $i --)
{
if ( $s > 9)
{
$res [ $i ] = 9;
$s -= 9;
}
else
{
$res [ $i ] = $s ;
$s = 0;
}
}
$res [0] = $s + 1;
echo "Smallest number is " ;
for ( $i = 0; $i < $m ; $i ++)
echo $res [ $i ];
}
$s = 9; $m = 2;
findSmallest( $m , $s );
?>
|
Javascript
<script>
function findSmallest(m, s)
{
if (s == 0)
{
(m == 1)? document.write( "Smallest number is " ) + 0
: document.write( "Not possible" );
return ;
}
if (s > 9*m)
{
document.write( "Not possible" );
return ;
}
let res = new Array(m);
s -= 1;
for (let i=m-1; i>0; i--)
{
if (s > 9)
{
res[i] = 9;
s -= 9;
}
else
{
res[i] = s;
s = 0;
}
}
res[0] = s + 1;
document.write( "Smallest number is " );
for (let i=0; i<m; i++)
document.write(res[i]);
}
let s = 9, m = 2;
findSmallest(m, s);
</script>
|
Output :
Smallest number is 18
Time Complexity: O(m), where m represents the value of given integer.
Auxiliary Space: O(m), where m represents the value of given integer.
We will soon be discussing approach to find the largest possible number with given sum of digits and number of digits.
Share your thoughts in the comments
Please Login to comment...