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.
- 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
- Ways to form an array having integers in given range such that total sum is divisible by 2
- 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
- All ways to add parenthesis for evaluation
- Count of different ways to express N as the sum of 1, 3 and 4
- Number of ways to reach the end of matrix with non-zero AND value
- Number of ways to pair people
- Different ways to sum n using numbers greater than or equal to m
- Count ways to reach the n'th stair
- Count possible ways to construct buildings
- Count ways to reach the nth stair using step 1, 2 or 3
Improved By : ChitraNayal