For a given number n > 0, find the number of different ways in which n can be written as a sum of at two or more positive integers.
Input : n = 5 Output : 6 Explanation : All possible six ways are : 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 2 + 1 + 1 + 1 1 + 1 + 1 + 1 + 1 Input : 4 Output : 4 Explanation : All possible four ways are : 3 + 1 2 + 2 2 + 1 + 1 1 + 1 + 1 + 1
This problem can be solved in the similar fashion as coin change problem, the difference is only that in this case we should iterate for 1 to n-1 instead of particular values of coin as in coin-change problem.
Time complexity O(n2)
This article is contributed by Shivam Pradhan (anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count of different ways to express N as the sum of 1, 3 and 4
- Number of ways to form a heap with n distinct integers
- Ways to sum to N using array elements with repetition allowed
- Modify array to maximize sum of adjacent differences
- Finding the maximum square sub-matrix with all equal elements
- Count binary strings with k times appearing adjacent two set bits
- Unbounded Knapsack (Repetition of items allowed)
- Find all distinct subset (or subsequence) sums of an array
- Maximum sum Bi-tonic Sub-sequence
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Minimum and Maximum values of an expression with * and +
- Program for nth Catalan Number
- Subset Sum Problem | DP-25
- 0-1 Knapsack Problem | DP-10
- Coin Change | DP-7