Count different numbers that can be generated such that there digits sum is equal to ‘n’
Last Updated :
09 May, 2022
Given an positive integer n. Count the different numbers that can be generated using digits 1, 2, 3 and 4 such that digits sum is the number ‘n’. Here digit ‘4’ will be treated as ‘1’. For instance,
32 = 3 + 2 = 5
1341 = 1 + 3 + 1 + 1 = 6
441 = 1 + 1 + 1 = 3
Note: Answer the value in mod = 109+7
Input: 2
Output: 5
Explanation
There are only '5' numbers that can
be made:
11 = 1 + 1 = 2
14 = 1 + 1 = 2
41 = 1 + 1 = 2
44 = 1 + 1 = 2
2 = 2
Input: 3
Output: 13
Explanation
There are only '13' numbers that can
be made i.e., 111, 114, 141, 144, 411,
414, 441, 444, 12, 21, 42, 24, 3.
The approach is to use Dynamic programming. The problem is same as coin change and Ways to write n as sum of two or more positive integers problems. The only difference is that, instead of iterating up-to ‘n’, iterate only from 1 to 3 as according to question, only 1, 2, 3 and 4 digits are allowed. But since ‘4’ can be replaced with ‘1’ therefore iterate through 1, 2 and 3 and double the count of ‘1’ for compensation of digit ‘4’.
C++
#include<iostream>
using namespace std;
int countWays( int num)
{
int dp[num+1];
const int MOD = 1e9 + 7;
dp[1] = 2;
for ( int i = 2; i <= num; ++i)
{
dp[i] = 0;
for ( int j = 1; j <= 3; ++j)
{
if (i - j == 0)
dp[i] += 1;
else if (j == 1)
dp[i] += dp[i-j] * 2;
else if (i - j > 0)
dp[i] += dp[i-j];
if (dp[i] >= MOD)
dp[i] %= MOD;
}
}
return dp[num];
}
int main()
{
int n = 3;
cout << countWays(n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int countWays( int num)
{
int []dp= new int [num + 1 ];
int MOD = ( int )1E9 + 7 ;
dp[ 1 ] = 2 ;
for ( int i = 2 ; i <= num; ++i)
{
dp[i] = 0 ;
for ( int j = 1 ; j <= 3 ; ++j)
{
if (i - j == 0 )
dp[i] += 1 ;
else if (j == 1 )
dp[i] += dp[i - j] * 2 ;
else if (i - j > 0 )
dp[i] += dp[i - j];
if (dp[i] >= MOD)
dp[i] %= MOD;
}
}
return dp[num];
}
static public void main (String[] args)
{
int n = 3 ;
System.out.println(countWays(n));
}
}
|
Python3
def countWays(num):
dp = [ 0 ] * (num + 1 );
MOD = 100000000 + 7 ;
dp[ 1 ] = 2 ;
for i in range ( 2 , num + 1 ):
dp[i] = 0 ;
for j in range ( 1 , 4 ):
if (i - j = = 0 ):
dp[i] + = 1 ;
elif (j = = 1 ):
dp[i] + = dp[i - j] * 2 ;
elif (i - j > 0 ):
dp[i] + = dp[i - j];
if (dp[i] > = MOD):
dp[i] % = MOD;
return dp[num];
n = 3 ;
print (countWays(n));
|
C#
using System;
public class GFG
{
static int countWays( int num)
{
int []dp= new int [num + 1];
int MOD = ( int )1E9 + 7;
dp[1] = 2;
for ( int i = 2; i <= num; ++i)
{
dp[i] = 0;
for ( int j = 1; j <= 3; ++j)
{
if (i - j == 0)
dp[i] += 1;
else if (j == 1)
dp[i] += dp[i - j] * 2;
else if (i - j > 0)
dp[i] += dp[i - j];
if (dp[i] >= MOD)
dp[i] %= MOD;
}
}
return dp[num];
}
static public void Main (String []args)
{
int n = 3;
Console.WriteLine(countWays(n));
}
}
|
PHP
<?php
function countWays( $num )
{
$dp [ $num + 1] = array ();
$MOD = 100000000 + 7;
$dp [1] = 2;
for ( $i = 2; $i <= $num ; ++ $i )
{
$dp [ $i ] = 0;
for ( $j = 1; $j <= 3; ++ $j )
{
if ( $i - $j == 0)
$dp [ $i ] += 1;
else if ( $j == 1)
$dp [ $i ] += $dp [ $i - $j ] * 2;
else if ( $i - $j > 0)
$dp [ $i ] += $dp [ $i - $j ];
if ( $dp [ $i ] >= $MOD )
$dp [ $i ] %= $MOD ;
}
}
return $dp [ $num ];
}
$n = 3;
echo countWays( $n );
?>
|
Javascript
<script>
function countWays(num)
{
let dp = [];
let MOD = 1E9 + 7;
dp[1] = 2;
for (let i = 2; i <= num; ++i)
{
dp[i] = 0;
for (let j = 1; j <= 3; ++j)
{
if (i - j == 0)
dp[i] += 1;
else if (j == 1)
dp[i] += dp[i - j] * 2;
else if (i - j > 0)
dp[i] += dp[i - j];
if (dp[i] >= MOD)
dp[i] %= MOD;
}
}
return dp[num];
}
let n = 3;
document.write(countWays(n));
</script>
|
Output
13
Time complexity: O(n)
Auxiliary space: O(n)
Note: Asked in Directi coding round(2014 and 2017)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...