Given a positive integer n, the task is to generate all possible unique ways to represent n as sum of positive integers.
2 1 1
1 1 1 1
1 1 1
Approach: We have already discussed the implementation of generating unique partitions in this post. This post contains another and far more intuitive implementation for the above problem using recursion.
The idea is simple and is kind of same approach as used here. We have to move recursively from n to 1 and keep on appending the numbers used to form sum in the array. When the sum becomes equal to n then we print the array and return. The base cases considered in the implementation are: remSum == 0: Required n is formed so print the array.
Then we have started forming required sum using the max value number used in the previous partition. If that number becomes greater than n we ignore it else we append that number to the array and move recursively to next iteration to form sum = (remSum – i) and where max value
number that could be used is i or less than i. This way we can generate the required unique partitions.
Below is the implementation of the above approach:
4 3 1 2 2 2 1 1 1 1 1 1
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.
- Generate all unique partitions of an integer
- Given a string, print all possible palindromic partitions
- Sum of elements of all partitions of number such that no element is less than K
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Generate all passwords from given character set
- All unique combinations whose sum equals to K
- Find sum of all unique elements in the array for K queries
- Count all possible unique sum of series K, K+1, K+2, K+3, K+4, ..., K+N
- Count of subarrays of an Array having all unique digits
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Generate all binary strings without consecutive 1's
- Generate all the binary strings of N bits
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Minimum number of coins that can generate all the values in the given range
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Generate all distinct subsequences of array using backtracking
- Generate all possible permutations of a Number divisible by N
- Bitmasking and Dynamic Programming | Set 1 (Count ways to assign unique cap to every person)
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.