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

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

DP_{n}= DP_{n-1}+ DP_{n-3}+ DP_{n-4}

Base case :DP[0] = DP[1] = DP[2] = 1 and DP[3] = 2

## C++

`// CPP 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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

64

**Time Complexity :** O(n)

**Auxiliary Space :** O(n)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Count ways to express 'n' as sum of odd integers
- Count ways to express even number ‘n’ as sum of even integers
- Count number of ways to get Odd Sum
- Count possible ways to construct buildings
- Count ways to reach the n'th stair
- Count number of ways to cover a distance
- Count number of ways to partition a set into k subsets
- Count ways to reach the nth stair using step 1, 2 or 3
- Count the number of ways to traverse a Matrix
- Count ways to increase LCS length of two strings by one
- Count number of ways to jump to reach end
- Count ways to reach a score using 1 and 2 with no consecutive 2s
- Count number of ways to arrange first N numbers
- Count ways to build street under given constraints
- Count ways to distribute m items among n people

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.