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 the number of ways to tile the floor of size n x m using 1 x m size tiles
- Count ways to reach the n'th stair
- Count number of ways to reach a given score in a game
- Count possible ways to construct buildings
- Bitmasking and Dynamic Programming | Set 1 (Count ways to assign unique cap to every person)
- Count number of ways to cover a distance
- Count number of ways to partition a set into k subsets
- Bell Numbers (Number of ways to Partition a Set)
- All ways to add parenthesis for evaluation
- Count number of ways to fill a "n x 4" grid using "1 x 4" tiles
- Ways to arrange Balls such that adjacent balls are of different types
- Count number of ways to reach destination in a Maze
- Count ways to increase LCS length of two strings by one
- Count number of ways to jump to reach end
- Ways to sum to N using array elements with repetition allowed
Improved By : chitranayal