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 email@example.com. 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.
- Ways to write N as sum of two or more positive integers | Set-2
- Number of arrays of size N whose elements are positive integers and sum is K
- Number of ways to form a heap with n distinct integers
- Longest alternating (positive and negative) subarray starting at every index
- Minimum time to write characters using insert, delete and copy operation
- Number of n-digits non-decreasing integers
- Count of different ways to express N as the sum of 1, 3 and 4
- All ways to add parenthesis for evaluation
- Count possible ways to construct buildings
- Count ways to reach the n'th stair
- Different ways to sum n using numbers greater than or equal to m
- Ways to place K bishops on an N×N chessboard so that no two attack
- Number of ways to arrange N items under given constraints
- Count the number of ways to traverse a Matrix
- Count ways to reach a score using 1 and 2 with no consecutive 2s