Find all combinations that add upto given number
Last Updated :
10 Apr, 2023
Given a positive number, find out all combinations of positive numbers that adds upto that number. The program should print only combinations, not permutations. For example, for input 3, either 1, 2 or 2, 1 should be printed.
Examples :
Input: N = 3
Output:
1 1 1
1 2
3
Input: N = 5
Output:
1 1 1 1 1
1 1 1 2
1 1 3
1 2 2
1 4
2 3
5
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to use recursion. We use an array to store combinations and we recursively fill the array and recurse with reduced number. The invariant used in the solution is that each combination will always be stored in increasing order of elements involved. That way we can avoid printing permutations.
Algorithm:
Step 1: Define a function named findCombinationsUtil which takes 4 parameters – arr[], index, num, and reducedNum.
Step 2: If the reducedNum is less than 0, return.
Step 3: If the reducedNum is equal to 0, print the array arr[] till index-1 and return.
Step 4: Find the previous element stored in arr[]. If index is 0, then prev = 1, else prev = arr[index – 1].
Step 5: Run a loop from prev to num.
Step 6: For each value k in the loop: a. Set the next element of arr[] as k, i.e., arr[index] = k. b. Call the findCombinationsUtil function recursively with parameters arr[], index+1, num, and reducedNum-k.
Below is implementation of above idea :
Java
import java.io.*;
class GFG
{
static void findCombinationsUtil( int arr[], int index,
int num, int reducedNum)
{
if (reducedNum < 0 )
return ;
if (reducedNum == 0 )
{
for ( int i = 0 ; i < index; i++)
System.out.print (arr[i] + " " );
System.out.println();
return ;
}
int prev = (index == 0 ) ?
1 : arr[index - 1 ];
for ( int k = prev; k <= num ; k++)
{
arr[index] = k;
findCombinationsUtil(arr, index + 1 , num,
reducedNum - k);
}
}
static void findCombinations( int n)
{
int arr[] = new int [n];
findCombinationsUtil(arr, 0 , n, n);
}
public static void main (String[] args)
{
int n = 5 ;
findCombinations(n);
}
}
|
C++
#include <iostream>
using namespace std;
void findCombinationsUtil( int arr[], int index,
int num, int reducedNum)
{
if (reducedNum < 0)
return ;
if (reducedNum == 0)
{
for ( int i = 0; i < index; i++)
cout << arr[i] << " " ;
cout << endl;
return ;
}
int prev = (index == 0)? 1 : arr[index-1];
for ( int k = prev; k <= num ; k++)
{
arr[index] = k;
findCombinationsUtil(arr, index + 1, num,
reducedNum - k);
}
}
void findCombinations( int n)
{
int arr[n];
findCombinationsUtil(arr, 0, n, n);
}
int main()
{
int n = 5;
findCombinations(n);
return 0;
}
|
Python3
def findCombinationsUtil(arr, index, num,
reducedNum):
if (reducedNum < 0 ):
return
if (reducedNum = = 0 ):
for i in range (index):
print (arr[i], end = " " )
print ("")
return
prev = 1 if (index = = 0 ) else arr[index - 1 ]
for k in range (prev, num + 1 ):
arr[index] = k
findCombinationsUtil(arr, index + 1 , num,
reducedNum - k)
def findCombinations(n):
arr = [ 0 ] * n
findCombinationsUtil(arr, 0 , n, n)
n = 5 ;
findCombinations(n);
|
C#
using System;
class GFG
{
static void findCombinationsUtil( int []arr, int index,
int num, int reducedNum)
{
if (reducedNum < 0)
return ;
if (reducedNum == 0)
{
for ( int i = 0; i < index; i++)
Console.Write (arr[i] + " " );
Console.WriteLine();
return ;
}
int prev = (index == 0) ?
1 : arr[index - 1];
for ( int k = prev; k <= num ; k++)
{
arr[index] = k;
findCombinationsUtil(arr, index + 1, num,
reducedNum - k);
}
}
static void findCombinations( int n)
{
int []arr = new int [n];
findCombinationsUtil(arr, 0, n, n);
}
static public void Main ()
{
int n = 5;
findCombinations(n);
}
}
|
PHP
<?php
function findCombinationsUtil( $arr , $index ,
$num , $reducedNum )
{
if ( $reducedNum < 0)
return ;
if ( $reducedNum == 0)
{
for ( $i = 0; $i < $index ; $i ++)
echo $arr [ $i ] , " " ;
echo "\n" ;
return ;
}
$prev = ( $index == 0) ? 1 : $arr [ $index - 1];
for ( $k = $prev ; $k <= $num ; $k ++)
{
$arr [ $index ] = $k ;
findCombinationsUtil( $arr , $index + 1,
$num , $reducedNum - $k );
}
}
function findCombinations( $n )
{
$arr = array ();
findCombinationsUtil( $arr , 0, $n , $n );
}
$n = 5;
findCombinations( $n );
?>
|
Javascript
<script>
function findCombinationsUtil(arr, index,
num, reducedNum)
{
if (reducedNum < 0)
return ;
if (reducedNum == 0)
{
for (let i = 0; i < index; i++)
document.write (arr[i] + " " );
document.write( "<br/>" );
return ;
}
let prev = (index == 0) ?
1 : arr[index - 1];
for (let k = prev; k <= num ; k++)
{
arr[index] = k;
findCombinationsUtil(arr, index + 1, num,
reducedNum - k);
}
}
function findCombinations(n)
{
let arr = [];
findCombinationsUtil(arr, 0, n, n);
}
let n = 5;
findCombinations(n);
</script>
|
Output
1 1 1 1 1
1 1 1 2
1 1 3
1 2 2
1 4
2 3
5
Exercise : Modify above solution to consider only distinct elements in a combination.
Time Complexity : O(2^n)
Auxiliary Space : O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...