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.
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.
- Ways to write N as sum of two or more positive integers | Set-2
- Number of ways to write N as a sum of K non-negative integers
- Number of arrays of size N whose elements are positive integers and sum is K
- Longest alternating subsequence in terms of positive and negative integers
- Ways of transforming one string to other by removing 0 or more characters
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Number of ways to form a heap with n distinct integers
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Represent a number as the sum of positive numbers ending with 9
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Remove minimum elements from either side such that 2*min becomes more than max
- Sequences of given length where every element is more than or equal to twice of previous
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Maximum sub-matrix area having count of 1's one more than count of 0's
- Probability of getting more heads than tails when N biased coins are tossed
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Count ways to increase LCS length of two strings by one
- Ways to place K bishops on an N×N chessboard so that no two attack
- Number of ways to partition a string into two balanced subsequences
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
Improved By : chitranayal