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.
Examples:
Input: N = 3
Output:
1 2
3
Input: N = 7
Output:
1 2 4
1 6
2 5
3 4
7
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:
C++
#include <bits/stdc++.h>
using namespace std;
void findCombinationsUtil( int arr[], int index,
int n, int red_num)
{
set< int > s;
int sum = 0;
if (red_num < 0) {
return ;
}
if (red_num == 0) {
for ( int i = 0; i < index; i++) {
s.insert(arr[i]);
}
for ( auto itr = s.begin();
itr != s.end(); itr++) {
sum = sum + (*itr);
}
if (sum == n) {
for ( auto i = s.begin();
i != s.end(); i++) {
cout << *i << " " ;
}
cout << endl;
return ;
}
}
int prev = (index == 0) ? 1 : arr[index - 1];
for ( int k = prev; k <= n; k++) {
arr[index] = k;
findCombinationsUtil(arr, index + 1,
n, red_num - k);
}
}
void findCombinations( int n)
{
int a[n];
findCombinationsUtil(a, 0, n, n);
}
int main()
{
int n = 7;
findCombinations(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findCombinationsUtil( int arr[], int index,
int n, int red_num)
{
HashSet<Integer> s = new HashSet<>();
int sum = 0 ;
if (red_num < 0 )
{
return ;
}
if (red_num == 0 )
{
for ( int i = 0 ; i < index; i++)
{
s.add(arr[i]);
}
for (Integer itr : s)
{
sum = sum + itr;
}
if (sum == n)
{
for (Integer i : s)
{
System.out.print(i+ " " );
}
System.out.println();
return ;
}
}
int prev = (index == 0 ) ? 1 : arr[index - 1 ];
for ( int k = prev; k <= n; k++)
{
if (index < n)
{
arr[index] = k;
findCombinationsUtil(arr, index + 1 ,
n, red_num - k);
}
}
}
static void findCombinations( int n)
{
int []a = new int [n];
findCombinationsUtil(a, 0 , n, n);
}
public static void main(String arr[])
{
int n = 7 ;
findCombinations(n);
}
}
|
Python3
def findCombinationsUtil(arr, index, n, red_num):
s = set ()
sum = 0
if (red_num < 0 ):
return
if (red_num = = 0 ):
for i in range (index):
s.add(arr[i])
for itr in s:
sum = sum + (itr)
if ( sum = = n):
for i in s:
print (i, end = " " )
print ( "\n" , end = "")
return
if (index = = 0 ):
prev = 1
else :
prev = arr[index - 1 ]
for k in range (prev, n + 1 , 1 ):
arr[index] = k
findCombinationsUtil(arr, index + 1 ,
n, red_num - k)
def findCombinations(n):
a = [ 0 for i in range (n + 1 )]
findCombinationsUtil(a, 0 , n, n)
if __name__ = = '__main__' :
n = 7
findCombinations(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findCombinationsUtil( int []arr, int index,
int n, int red_num)
{
HashSet< int > s = new HashSet< int >();
int sum = 0;
if (red_num < 0)
{
return ;
}
if (red_num == 0)
{
for ( int i = 0; i < index; i++)
{
s.Add(arr[i]);
}
foreach ( int itr in s)
{
sum = sum + itr;
}
if (sum == n)
{
foreach ( int i in s)
{
Console.Write(i+ " " );
}
Console.WriteLine();
return ;
}
}
int prev = (index == 0) ? 1 : arr[index - 1];
for ( int k = prev; k <= n; k++)
{
if (index < n)
{
arr[index] = k;
findCombinationsUtil(arr, index + 1,
n, red_num - k);
}
}
}
static void findCombinations( int n)
{
int []a = new int [n];
findCombinationsUtil(a, 0, n, n);
}
public static void Main(String []arr)
{
int n = 7;
findCombinations(n);
}
}
|
Javascript
<script>
function findCombinationsUtil(arr, index, n, red_num) {
var s = new Set();
var sum = 0;
if (red_num < 0) {
return ;
}
if (red_num === 0) {
for ( var i = 0; i < index; i++) {
s.add(arr[i]);
}
for (const itr of s) {
sum = sum + itr;
}
if (sum === n) {
for (const i of s) {
document.write(i + " " );
}
document.write( "<br>" );
return ;
}
}
var prev = index === 0 ? 1 : arr[index - 1];
for ( var k = prev; k <= n; k++) {
if (index < n) {
arr[index] = k;
findCombinationsUtil(arr, index + 1, n, red_num - k);
}
}
}
function findCombinations(n) {
var a = new Array(n).fill(0);
findCombinationsUtil(a, 0, n, n);
}
var n = 7;
findCombinations(n);
</script>
|
Output:
1 2 4
1 6
2 5
3 4
7
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 May, 2022
Like Article
Save Article