Print all n-digit numbers whose sum of digits equals to given sum
Given number of digits n, print all n-digit numbers whose sum of digits adds upto given sum. Solution should not consider leading 0’s as digits.
Examples:
Input: N = 2, Sum = 3
Output: 12 21 30
Input: N = 3, Sum = 6
Output: 105 114 123 132 141 150 204
213 222 231 240 303 312 321
330 402 411 420 501 510 600
Input: N = 4, Sum = 3
Output: 1002 1011 1020 1101 1110 1200
2001 2010 2100 3000
A simple solution would be to generate all N-digit numbers and print numbers that have sum of their digits equal to given sum. The complexity of this solution would be exponential.
A better solution is to generate only those N-digit numbers that satisfy the given constraints. The idea is to use recursion. We basically fill all digits from 0 to 9 into current position and maintain sum of digits so far. We then recurse for remaining sum and number of digits left. We handle leading 0’s separately as they are not counted as digits.
Below is a simple recursive implementation of above idea –
C++
#include <bits/stdc++.h>
using namespace std;
void findNDigitNumsUtil( int n, int sum, char * out,
int index)
{
if (index > n || sum < 0)
return ;
if (index == n)
{
if (sum == 0)
{
out[index] = '\0' ;
cout << out << " " ;
}
return ;
}
for ( int i = 0; i <= 9; i++)
{
out[index] = i + '0' ;
findNDigitNumsUtil(n, sum - i, out, index + 1);
}
}
void findNDigitNums( int n, int sum)
{
char out[n + 1];
for ( int i = 1; i <= 9; i++)
{
out[0] = i + '0' ;
findNDigitNumsUtil(n, sum - i, out, 1);
}
}
int main()
{
int n = 2, sum = 3;
findNDigitNums(n, sum);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findNDigitNumsUtil( int n, int sum, char out[],
int index)
{
if (index > n || sum < 0 )
return ;
if (index == n)
{
if (sum == 0 )
{
out[index] = '\0' ;
System.out.print(out);
System.out.print( " " );
}
return ;
}
for ( int i = 0 ; i <= 9 ; i++)
{
out[index] = ( char )(i + '0' );
findNDigitNumsUtil(n, sum - i, out, index + 1 );
}
}
static void findNDigitNums( int n, int sum)
{
char [] out = new char [n + 1 ];
for ( int i = 1 ; i <= 9 ; i++)
{
out[ 0 ] = ( char )(i + '0' );
findNDigitNumsUtil(n, sum - i, out, 1 );
}
}
public static void main (String[] args)
{
int n = 2 , sum = 3 ;
findNDigitNums(n, sum);
}
}
|
Python 3
def findNDigitNumsUtil(n, sum , out,index):
if (index > n or sum < 0 ):
return
f = ""
if (index = = n):
if ( sum = = 0 ):
out[index] = "\0"
for i in out:
f = f + i
print (f, end = " " )
return
for i in range ( 10 ):
out[index] = chr (i + ord ( '0' ))
findNDigitNumsUtil(n, sum - i,
out, index + 1 )
def findNDigitNums( n, sum ):
out = [ False ] * (n + 1 )
for i in range ( 1 , 10 ):
out[ 0 ] = chr (i + ord ( '0' ))
findNDigitNumsUtil(n, sum - i, out, 1 )
if __name__ = = "__main__" :
n = 2
sum = 3
findNDigitNums(n, sum )
|
C#
using System;
class GFG {
static void findNDigitNumsUtil( int n, int sum,
char []ou, int index)
{
if (index > n || sum < 0)
return ;
if (index == n)
{
if (sum == 0)
{
ou[index] = '\0' ;
Console.Write(ou);
Console.Write( " " );
}
return ;
}
for ( int i = 0; i <= 9; i++)
{
ou[index] = ( char )(i + '0' );
findNDigitNumsUtil(n, sum - i, ou,
index + 1);
}
}
static void findNDigitNums( int n, int sum)
{
char []ou = new char [n + 1];
for ( int i = 1; i <= 9; i++)
{
ou[0] = ( char )(i + '0' );
findNDigitNumsUtil(n, sum - i, ou, 1);
}
}
public static void Main ()
{
int n = 2, sum = 3;
findNDigitNums(n, sum);
}
}
|
Javascript
<script>
function findNDigitNumsUtil(n, sum, out, index)
{
if (index > n || sum < 0)
return ;
if (index == n)
{
if (sum == 0)
{
out[index] = '\0' ;
for (let i = 0; i < out.length; i++)
document.write(out[i]);
document.write( " " );
}
return ;
}
for (let i = 0; i <= 9; i++)
{
out[index] = String.fromCharCode(i + '0' .charCodeAt(0));
findNDigitNumsUtil(n, sum - i, out, index + 1);
}
}
function findNDigitNums(n,sum)
{
let out = new Array(n+1);
for (let i=0;i<n+1;i++)
{
out[i]= false ;
}
for (let i = 1; i <= 9; i++)
{
out[0] = String.fromCharCode(i + '0' .charCodeAt(0));
findNDigitNumsUtil(n, sum - i, out, 1);
}
}
let n = 2, sum = 3;
findNDigitNums(n, sum);
</script>
|
PHP
<?php
function findNDigitNumsUtil( $n , $sum , $out ,
$index )
{
if ( $index > $n || $sum < 0)
return ;
if ( $index == $n )
{
if ( $sum == 0)
{
$out [ $index ] = '' ;
foreach ( $out as & $value )
print ( $value );
print ( " " );
}
return ;
}
for ( $i = 0; $i <= 9; $i ++)
{
$out [ $index ] = chr ( $i + ord( '0' ));
findNDigitNumsUtil( $n , $sum - $i ,
$out , $index + 1);
}
}
function findNDigitNums( $n , $sum )
{
$out = array_fill (0, $n + 1, false);
for ( $i = 1; $i <= 9; $i ++)
{
$out [0] = chr ( $i + ord( '0' ));
findNDigitNumsUtil( $n , $sum - $i , $out , 1);
}
}
$n = 2;
$sum = 3;
findNDigitNums( $n , $sum );
?>
|
Output:
12 21 30
Time Complexity: O(n*n!)
Auxiliary Space: O(n)
Last Updated :
07 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...