Open In App

Count of different ways to express N as the sum of 1, 3 and 4

Last Updated : 29 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to illustrate the number of
// ways to represent N as sum of 1, 3 and 4.
#include <bits/stdc++.h>
using namespace std;
  
// function to count the number of
// ways to represent n as sum of 1, 3 and 4
int countWays(int n)
{
    int DP[n + 1];
  
    // base cases
    DP[0] = DP[1] = DP[2] = 1;
    DP[3] = 2;
  
    // iterate for all values from 4 to n
    for (int i = 4; i <= n; i++) 
        DP[i] = DP[i - 1] + DP[i - 3] + DP[i - 4];
      
    return DP[n];
}
  
// driver code
int main()
{
    int n = 10;
    cout << countWays(n);
    return 0;
}


Java




// Java program to illustrate 
// the number of ways to represent 
// N as sum of 1, 3 and 4.
  
class GFG {
  
    // Function to count the 
    // number of ways to represent 
    // n as sum of 1, 3 and 4
    static int countWays(int n)
    {
        int DP[] = new int[n + 1];
  
        // base cases
        DP[0] = DP[1] = DP[2] = 1;
        DP[3] = 2;
  
        // iterate for all values from 4 to n
        for (int i = 4; i <= n; i++)
            DP[i] = DP[i - 1] + DP[i - 3
                    + DP[i - 4];
  
        return DP[n];
    }
  
    // driver code
    public static void main(String[] args)
    {
        int n = 10;
        System.out.println(countWays(n));
    }
}
  
// This code is contributed 
// by prerna saini.


Python3




# Python program to illustrate the number of
# ways to represent N as sum of 1, 3 and 4.
  
# Function to count the number of
# ways to represent n as sum of 1, 3 and 4
def countWays(n):
  
    DP = [0 for i in range(0, n + 1)]
      
    # base cases
    DP[0] = DP[1] = DP[2] = 1
    DP[3] = 2
  
    # Iterate for all values from 4 to n
    for i in range(4, n + 1):
        DP[i] = DP[i - 1] + DP[i - 3] + DP[i - 4]
      
    return DP[n]
  
      
# Driver code 
n = 10
print (countWays(n))
  
# This code is contributed by Gitanjali.


C#




// C# program to illustrate 
// the number of ways to represent 
// N as sum of 1, 3 and 4.
using System;
  
class GFG {
  
    // Function to count the 
    // number of ways to represent 
    // n as sum of 1, 3 and 4
    static int countWays(int n)
    {
        int []DP = new int[n + 1];
  
        // base cases
        DP[0] = DP[1] = DP[2] = 1;
        DP[3] = 2;
  
        // iterate for all values from 4 to n
        for (int i = 4; i <= n; i++)
            DP[i] = DP[i - 1] + DP[i - 3] 
                    + DP[i - 4];
  
        return DP[n];
    }
  
    // Driver code
    public static void Main()
    {
        int n = 10;
        Console.WriteLine(countWays(n));
    }
}
  
// This code is contributed 
// by vt_m.


PHP




<?php
// PHP program to illustrate 
// the number of ways to 
// represent N as sum of 
// 1, 3 and 4.
  
// function to count the 
// number of ways to 
// represent n as sum of
// 1, 3 and 4
function countWays($n)
{
    $DP = array();
  
    // base cases
    $DP[0] = $DP[1] = $DP[2] = 1;
    $DP[3] = 2;
  
    // iterate for all 
    // values from 4 to n
    for ($i = 4; $i <= $n; $i++) 
        $DP[$i] = $DP[$i - 1] + 
                  $DP[$i - 3] + 
                  $DP[$i - 4];
      
    return $DP[$n];
}
  
// Driver Code
$n = 10;
echo countWays($n);
  
// This code is contributed
// by Sam007
?>


Javascript




<script>
  
// Javascript program to illustrate
// the number of ways to represent
// N as sum of 1, 3 and 4.
  
// Function to count the
// number of ways to represent
// n as sum of 1, 3 and 4
function countWays(n)
{
    var DP = [];
    DP.length = 10;
    DP.fill(0);
  
    // Base cases
    DP[0] = DP[1] = DP[2] = 1;
    DP[3] = 2;
  
    // Iterate for all values from 4 to n
    for(var i = 4; i <= n; i++)
        DP[i] = DP[i - 1] + DP[i - 3] + 
                DP[i - 4];
  
    return DP[n];
}
  
// Driver code
var n = 10;
  
document.write(countWays(n));
  
// This code is contributed by bunnyram19   
  
</script>


Output

64

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++




// C++ program to illustrate the number of ways to represent N as sum of 1, 3 and 4.
  
#include <bits/stdc++.h>
using namespace std;
  
// function to count the number of ways to represent n as sum of 1, 3 and 4
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;
  
    // base cases
    dp_i_4 = dp_i_3 = dp_i_2 = 1;
    dp_i_1 = 2;
  
    // iterate for all values from 4 to n
    for (int i = 4; i <= n; i++) {
        dp_i = dp_i_1 + dp_i_3 + dp_i_4;
        // Updating Variable value so in next Iteration they become relevant 
        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;
}
  
// driver code
int main() {
    int n = 10;
    cout << countWays(n);
    return 0;
}


Java




import java.util.*;
  
public class Main {
    // function to count the number of ways to represent n as sum of 1, 3 and 4
    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;
  
        // base cases
        dp_i_4 = dp_i_3 = dp_i_2 = 1;
        dp_i_1 = 2;
  
        // iterate for all values from 4 to n
        for (int i = 4; i <= n; i++) {
            dp_i = dp_i_1 + dp_i_3 + dp_i_4;
            // Updating Variable value so in next Iteration they become relevant 
            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;
    }
  
    // driver code
    public static void main(String[] args) {
        int n = 10;
        System.out.println(countWays(n));
    }
}


Python3




# Python program to illustrate the number of ways to represent N as sum of 1, 3 and 4.
  
# function to count the number of ways to represent n as sum of 1, 3 and 4
def countWays(n):
    if n == 0 or n == 1 or n == 2:
        return 1
    elif n == 3:
        return 2
  
    # base cases
    dp_i_4 = dp_i_3 = dp_i_2 = 1
    dp_i_1 = 2
  
    # iterate for all values from 4 to n
    for i in range(4, n+1):
        dp_i = dp_i_1 + dp_i_3 + dp_i_4
        # Updating Variable value so in next Iteration they become relevant 
        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
  
# driver code
n = 10
print(countWays(n))


C#




using System;
  
public class GFG
{
    // function to count the number of ways to represent n as sum of 1, 3 and 4
    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;
  
        // base cases
        dp_i_4 = dp_i_3 = dp_i_2 = 1;
        dp_i_1 = 2;
  
        // iterate for all values from 4 to n
        for (int i = 4; i <= n; i++)
        {
            dp_i = dp_i_1 + dp_i_3 + dp_i_4;
            // Updating Variable value so in next Iteration they become relevant 
            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;
    }
  
    // driver code
    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;
  
    // base cases
    dp_i_4 = dp_i_3 = dp_i_2 = 1;
    dp_i_1 = 2;
  
    // iterate for all values from 4 to n
    for (let i = 4; i <= n; i++) {
        dp_i = dp_i_1 + dp_i_3 + dp_i_4;
        // Updating Variable value so in next Iteration they become relevant 
        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;
}
  
// driver code
let n = 10;
console.log(countWays(n));


Output

64

Time Complexity : O(n) 
Auxiliary Space : O(1)



Previous Article
Next Article

Similar Reads

Ways to express a number as product of two different factors
Given a number n, write a program to calculate the number of ways in which numbers can be expressed as the product of two different factors. Examples: Input : 12 Output : 3 12 can be expressed as 1 * 12, 2 * 6 and 3*4. Input : 36 Output : 4 36 can be expressed as 1 * 36, 2 * 18, 3 * 12 and 4 * 9.All factors of 12 are = 1, 2, 3, 4, 6, 12 We can obse
5 min read
Count ways to express a number as sum of exactly two numbers
Given a positive integer N. The task is to find the number of ways in which you can express N as a sum of exactly two numbers A and B (N = A + B) where A &gt; 0, B &gt; 0 and B &gt; A. Examples: Input: N = 8 Output: 3 Explanation: N = 8 can be expressed as (1, 7), (2, 6), (3, 5) Input: N = 14 Output: 6 Approach: An observation here is that for ever
3 min read
Count ways to express a number as sum of consecutive numbers
Given an integer N, the task is to find the number of ways to represent this number as a sum of 2 or more consecutive natural numbers. Examples: Input: N = 15 Output: 3 Explanation: 15 can be represented as: 1 + 2 + 3 + 4 + 54 + 5 + 67 + 8 Input: N = 10 Output: 1 Recommended PracticeCount of sum of consecutivesTry It! Approach: The idea is to repre
5 min read
Count ways to express a number as sum of powers
Given two integers x and n, we need to find number of ways to express x as sum of n-th powers of unique natural numbers. It is given that 1 &lt;= n &lt;= 20.Examples: Input : x = 100 n = 2 Output : 3 Explanation: There are three ways to express 100 as sum of natural numbers raised to power 2. 100 = 10^2 = 8^2+6^2 = 1^2+3^2+4^2+5^2+7^2 Input : x = 1
5 min read
Count ways to express 'n' as sum of odd integers
Given an positive integer n. Count total number of ways to express 'n' as sum of odd positive integers. Examples: Input: 4 Output: 3 Explanation There are only three ways to write 4 as sum of odd integers: 1. 1 + 3 2. 3 + 1 3. 1 + 1 + 1 + 1 Input: 5 Output: 5 Simple approach is to find recursive nature of problem. The number 'n' can be written as s
6 min read
Count ways to express even number ā€˜nā€™ as sum of even integers
Given an positive even integer 'n'. Count total number of ways to express ā€˜nā€™ as sum of even positive integers. Output the answer in modulo 109 + 7Examples: Input: 6 Output: 4 Explanation There are only four ways to write 6 as sum of even integers: 1. 2 + 2 + 2 2. 2 + 4 3. 4 + 2 4. 6 Input: 8 Output: 8 Approach is to find pattern or recursive funct
7 min read
Count of ways N elements can form two different sets containing N/2 elements each
Given a number N, representing count of elements and is equal to 2K, the task is to find the number of ways these elements can be form 2 sets containing K elements each. Examples: Input: N = 4Output: 3Explanation: The 3 sets consisting of 2 (= N/2) elements are: [1, 2], [3, 4] [1, 3], [2, 4] [1, 4], [2, 3] Input: N = 20Output: 12164510040883200 App
4 min read
Count ways to form N sized Strings with at most two adjacent different pair
Given a value N, the task is to find the number of ways a binary string of size N can be formed such that at most 2 adjacent pairs have different values. Examples: Input: N = 1Output: 2Explanation: Possible ways to form string are "1", "0". So the answer is 2 Input: N = 2Output: 4Explanation: Possible ways are "10", "01", "11", "00". So the answer
5 min read
Count ways to select three indices from Binary String with different adjacent digits
Given a binary string S of length N, the task is to find the number of ways to select three indices such that if they are arranged in increasing order of indices no two adjacent indices will have the same value. Examples: Input: S = "00110"Output: 4Explanation: Below are the possible valid indices:=&gt; {0, 2, 4} from "00110" forms "010"=&gt; {0, 3
10 min read
Different ways to sum n using numbers greater than or equal to m
Given two natural number n and m. The task is to find the number of ways in which the numbers that are greater than or equal to m can be added to get the sum n. Examples: Input : n = 3, m = 1 Output : 3 Following are three different ways to get sum n such that each term is greater than or equal to m 1 + 1 + 1, 1 + 2, 3 Input : n = 2, m = 1 Output :
7 min read