Given a positive integer N, the task is to find out all the combinations of positive integers that add upto the given integer N. The program should print only combinations, not permutations and all the integers in a combination must be distinct. For example, for input 3, either 1, 2 or 2, 1 should be printed and 1, 1, 1 must not be printed as the integers are not distinct.
Input: N = 3
Input: N = 7
1 2 4
Approach: The approach is an extension of the approach discussed here. The idea used to get all the distinct element is that first we find all the elements that add up to give sum N. Then we iterate over each of the elements and store the elements into the set. Storing the elements into set would remove all the duplicate elements, and after that we add up the sum of the elements of the set and check whether it is equal to N or not.
Below is the implementation of the above approach:
1 2 4 1 6 2 5 3 4 7
- Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Print all combinations of balanced parentheses
- Print all combinations of factors (Ways to factorize)
- Print all combinations of points that can compose a given number
- Print all possible combinations of r elements in a given array of size n
- Numbers less than N which are product of exactly two distinct prime numbers
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Print all distinct permutations of a given string with duplicates
- Print All Distinct Elements of a given integer array
- Print sorted distinct elements of array
- Print all distinct circular strings of length M in lexicographical order
- Maximum sum of distinct numbers with LCM as N
- Exactly n distinct prime factor numbers from a to b
- Numbers having Unique (or Distinct) digits
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.