Given N, count the number of ways to express N as sum of 1, 3 and 4.
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:
DPn = DPn-1 + DPn-3 + DPn-4
Base case : DP = DP = DP = 1 and DP = 2
Time Complexity : O(n)
Auxiliary Space : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count ways to express 'n' as sum of odd integers
- Count ways to express even number ‘n’ as sum of even integers
- Different ways to sum n using numbers greater than or equal to m
- Different ways to represent N as sum of K non-zero integers
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Ways to arrange Balls such that adjacent balls are of different types
- Ways to select one or more pairs from two different sets
- Ways of dividing a group into two halves such that two elements are in different groups
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
- Number of ways to arrange K different objects taking N objects at a time
- 3 Different ways to print Fibonacci series in Java
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Minimum numbers needed to express every integer below N as a sum
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of palindromes required to express N as a sum | Set 2
- Count number of ways to get Odd Sum
- Count of ways in which N can be represented as sum of Fibonacci numbers without repetition
- Count of Ways to obtain given Sum from the given Array elements
- Count ways to obtain given sum by repeated throws of a dice
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : Sam007