Given N, count the number of ways to express N as sum of 1, 3 and 4.
Examples:
Input : N = 4
Output : 4
Explanation: 1+1+1+1
1+3
3+1
4
Input : N = 5
Output : 6
Explanation: 1 + 1 + 1 + 1 + 1
1 + 4
4 + 1
1 + 1 + 3
1 + 3 + 1
3 + 1 + 1
Approach : Divide the problem into sub-problems for solving it. Let DP[n] be the number of ways to write N as the sum of 1, 3, and 4. Consider one possible solution with n = x1 + x2 + x3 + … xn. If the last number is 1, then sum of the remaining numbers is n-1. So the number that ends with 1 is equal to DP[n-1]. Taking other cases into account where the last number is 3 and 4. The final recurrence would be:
DPn = DPn-1 + DPn-3 + DPn-4
Base case :
DP[0] = DP[1] = DP[2] = 1 and DP[3] = 2
C++
#include <bits/stdc++.h>
using namespace std;
int countWays( int n)
{
int DP[n + 1];
DP[0] = DP[1] = DP[2] = 1;
DP[3] = 2;
for ( int i = 4; i <= n; i++)
DP[i] = DP[i - 1] + DP[i - 3] + DP[i - 4];
return DP[n];
}
int main()
{
int n = 10;
cout << countWays(n);
return 0;
}
|
Java
class GFG {
static int countWays( int n)
{
int DP[] = new int [n + 1 ];
DP[ 0 ] = DP[ 1 ] = DP[ 2 ] = 1 ;
DP[ 3 ] = 2 ;
for ( int i = 4 ; i <= n; i++)
DP[i] = DP[i - 1 ] + DP[i - 3 ]
+ DP[i - 4 ];
return DP[n];
}
public static void main(String[] args)
{
int n = 10 ;
System.out.println(countWays(n));
}
}
|
Python3
def countWays(n):
DP = [ 0 for i in range ( 0 , n + 1 )]
DP[ 0 ] = DP[ 1 ] = DP[ 2 ] = 1
DP[ 3 ] = 2
for i in range ( 4 , n + 1 ):
DP[i] = DP[i - 1 ] + DP[i - 3 ] + DP[i - 4 ]
return DP[n]
n = 10
print (countWays(n))
|
C#
using System;
class GFG {
static int countWays( int n)
{
int []DP = new int [n + 1];
DP[0] = DP[1] = DP[2] = 1;
DP[3] = 2;
for ( int i = 4; i <= n; i++)
DP[i] = DP[i - 1] + DP[i - 3]
+ DP[i - 4];
return DP[n];
}
public static void Main()
{
int n = 10;
Console.WriteLine(countWays(n));
}
}
|
PHP
<?php
function countWays( $n )
{
$DP = array ();
$DP [0] = $DP [1] = $DP [2] = 1;
$DP [3] = 2;
for ( $i = 4; $i <= $n ; $i ++)
$DP [ $i ] = $DP [ $i - 1] +
$DP [ $i - 3] +
$DP [ $i - 4];
return $DP [ $n ];
}
$n = 10;
echo countWays( $n );
?>
|
Javascript
<script>
function countWays(n)
{
var DP = [];
DP.length = 10;
DP.fill(0);
DP[0] = DP[1] = DP[2] = 1;
DP[3] = 2;
for ( var i = 4; i <= n; i++)
DP[i] = DP[i - 1] + DP[i - 3] +
DP[i - 4];
return DP[n];
}
var n = 10;
document.write(countWays(n));
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(n)
Method – 2: Constant Space Complexity Solution (O(1) space complexity)
In the above solution we are storing all the dp states, i.e. we create n + 1 size dp array, but to calculate the particular dp[i[4] state we only need the dp[i-1], dp[i-3] and dp[i-4] states. so we can use this observation and conclude that we only store the previous 4 states to calculate the current dp[i] state.
so here we use
dp_i which store dp[i]
dp_i_1 which store dp[i-1]
dp_i_2 which store dp[i-2]
dp_i_3 which store dp[i-3]
dp_i_4 which store dp[i-4]
so in this solution we only need 5 variable so space complexity = O(5) ~= O(1)
below is the Implementation for this approach
C++
#include <bits/stdc++.h>
using namespace std;
int countWays( int n) {
int dp_i, dp_i_1, dp_i_2, dp_i_3, dp_i_4;
if (n == 0 || n == 1 || n == 2) return 1;
else if (n == 3) return 2;
dp_i_4 = dp_i_3 = dp_i_2 = 1;
dp_i_1 = 2;
for ( int i = 4; i <= n; i++) {
dp_i = dp_i_1 + dp_i_3 + dp_i_4;
dp_i_4 = dp_i_3;
dp_i_3 = dp_i_2;
dp_i_2 = dp_i_1;
dp_i_1 = dp_i;
}
return dp_i;
}
int main() {
int n = 10;
cout << countWays(n);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int countWays( int n) {
int dp_i = 0 , dp_i_1, dp_i_2, dp_i_3, dp_i_4;
if (n == 0 || n == 1 || n == 2 ) return 1 ;
else if (n == 3 ) return 2 ;
dp_i_4 = dp_i_3 = dp_i_2 = 1 ;
dp_i_1 = 2 ;
for ( int i = 4 ; i <= n; i++) {
dp_i = dp_i_1 + dp_i_3 + dp_i_4;
dp_i_4 = dp_i_3;
dp_i_3 = dp_i_2;
dp_i_2 = dp_i_1;
dp_i_1 = dp_i;
}
return dp_i;
}
public static void main(String[] args) {
int n = 10 ;
System.out.println(countWays(n));
}
}
|
Python3
def countWays(n):
if n = = 0 or n = = 1 or n = = 2 :
return 1
elif n = = 3 :
return 2
dp_i_4 = dp_i_3 = dp_i_2 = 1
dp_i_1 = 2
for i in range ( 4 , n + 1 ):
dp_i = dp_i_1 + dp_i_3 + dp_i_4
dp_i_4, dp_i_3, dp_i_2, dp_i_1 = dp_i_3, dp_i_2, dp_i_1, dp_i
return dp_i
n = 10
print (countWays(n))
|
C#
using System;
public class GFG
{
public static int CountWays( int n)
{
int dp_i = 0, dp_i_1, dp_i_2, dp_i_3, dp_i_4;
if (n == 0 || n == 1 || n == 2) return 1;
else if (n == 3) return 2;
dp_i_4 = dp_i_3 = dp_i_2 = 1;
dp_i_1 = 2;
for ( int i = 4; i <= n; i++)
{
dp_i = dp_i_1 + dp_i_3 + dp_i_4;
dp_i_4 = dp_i_3;
dp_i_3 = dp_i_2;
dp_i_2 = dp_i_1;
dp_i_1 = dp_i;
}
return dp_i;
}
public static void Main( string [] args)
{
int n = 10;
Console.WriteLine(CountWays(n));
}
}
|
Javascript
function countWays(n) {
let dp_i, dp_i_1, dp_i_2, dp_i_3, dp_i_4;
if (n == 0 || n == 1 || n == 2) return 1;
else if (n == 3) return 2;
dp_i_4 = dp_i_3 = dp_i_2 = 1;
dp_i_1 = 2;
for (let i = 4; i <= n; i++) {
dp_i = dp_i_1 + dp_i_3 + dp_i_4;
dp_i_4 = dp_i_3;
dp_i_3 = dp_i_2;
dp_i_2 = dp_i_1;
dp_i_1 = dp_i;
}
return dp_i;
}
let n = 10;
console.log(countWays(n));
|
Time Complexity : O(n)
Auxiliary Space : O(1)
Please Login to comment...