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
| // Java program to find out // all combinations of positive// numbers that add upto given// numberimportjava.io.*;classGFG {    /* arr - array to store the     combination     index - next location in array    num - given number    reducedNum - reduced number */staticvoidfindCombinationsUtil(intarr[], intindex,                                 intnum, intreducedNum){    // Base condition    if(reducedNum < 0)        return;    // If combination is     // found, print it    if(reducedNum == 0)    {        for(inti = 0; i < index; i++)                System.out.print (arr[i] + " ");            System.out.println();        return;    }    // Find the previous number     // stored in arr[]. It helps     // in maintaining increasing     // order    intprev = (index == 0) ?                           1: arr[index - 1];    // note loop starts from     // previous number i.e. at    // array location index - 1    for(intk = prev; k <= num ; k++)    {        // next element of        // array is k        arr[index] = k;        // call recursively with        // reduced number        findCombinationsUtil(arr, index + 1, num,                                 reducedNum - k);    }}/* Function to find out all combinations of positive numbers that add upto givennumber. It uses findCombinationsUtil() */staticvoidfindCombinations(intn){    // array to store the combinations    // It can contain max n elements    intarr[] = newint[n];    // find all combinations    findCombinationsUtil(arr, 0, n, n);}// Driver codepublicstaticvoidmain (String[] args) {    intn = 5;    findCombinations(n);}}// This code is contributed// by akt_mit | 
C++
| // C++ program to find out all combinations of// positive numbers that add upto given number#include <iostream>usingnamespacestd;/*    arr - array to store the combination    index - next location in array    num - given number    reducedNum - reduced number */voidfindCombinationsUtil(intarr[], intindex,                       intnum, intreducedNum){    // Base condition    if(reducedNum < 0)        return;    // If combination is found, print it    if(reducedNum == 0)    {        for(inti = 0; i < index; i++)            cout << arr[i] << " ";        cout << endl;        return;    }    // Find the previous number stored in arr[]    // It helps in maintaining increasing order    intprev = (index == 0)? 1 : arr[index-1];    // note loop starts from previous number    // i.e. at array location index - 1    for(intk = prev; k <= num ; k++)    {        // next element of array is k        arr[index] = k;        // call recursively with reduced number        findCombinationsUtil(arr, index + 1, num,                                 reducedNum - k);    }}/* Function to find out all combinations of   positive numbers that add upto given number.   It uses findCombinationsUtil() */voidfindCombinations(intn){    // array to store the combinations    // It can contain max n elements    intarr[n];    //find all combinations    findCombinationsUtil(arr, 0, n, n);}// Driver codeintmain(){    intn = 5;    findCombinations(n);    return0;} | 
Python3
| # Python3 program to find out all# combinations of positive # numbers that add upto given number# arr - array to store the combination# index - next location in array# num - given number# reducedNum - reduced number deffindCombinationsUtil(arr, index, num,                              reducedNum):    # Base condition    if(reducedNum < 0):        return    # If combination is     # found, print it    if(reducedNum ==0):        fori inrange(index):            print(arr[i], end =" ")        print("")        return    # Find the previous number stored in arr[].     # It helps in maintaining increasing order    prev =1if(index ==0) elsearr[index -1]    # note loop starts from previous     # number i.e. at array location    # index - 1    fork inrange(prev, num +1):                # next element of array is k        arr[index] =k        # call recursively with        # reduced number        findCombinationsUtil(arr, index +1, num,                                  reducedNum -k)# Function to find out all # combinations of positive numbers # that add upto given number.# It uses findCombinationsUtil() deffindCombinations(n):        # array to store the combinations    # It can contain max n elements    arr =[0] *n    # find all combinations    findCombinationsUtil(arr, 0, n, n)# Driver coden =5;findCombinations(n);# This code is contributed by mits | 
C#
| // C# program to find out all// combinations of positive numbers// that add upto given numberusingSystem;classGFG{/* arr - array to store the combination index - next location in array num - given number reducedNum - reduced number */staticvoidfindCombinationsUtil(int[]arr, intindex,                                  intnum, intreducedNum) {     // Base condition     if(reducedNum < 0)         return;     // If combination is     // found, print it     if(reducedNum == 0)     {         for(inti = 0; i < index; i++)             Console.Write (arr[i] + " ");             Console.WriteLine();         return;     }     // Find the previous number     // stored in arr[]. It helps     // in maintaining increasing     // order     intprev = (index == 0) ?                           1 : arr[index - 1];     // note loop starts from     // previous number i.e. at     // array location index - 1     for(intk = prev; k <= num ; k++)     {         // next element of         // array is k         arr[index] = k;         // call recursively with         // reduced number         findCombinationsUtil(arr, index + 1, num,                                  reducedNum - k);     } } /* Function to find out all combinations of positive numbers that add upto given number. It uses findCombinationsUtil() */staticvoidfindCombinations(intn) {     // array to store the combinations     // It can contain max n elements     int[]arr = newint[n];     // find all combinations     findCombinationsUtil(arr, 0, n, n); } // Driver code staticpublicvoidMain (){    intn = 5;     findCombinations(n); } } // This code is contributed // by akt_mit  | 
PHP
| <?php// PHP program to find out all// combinations of positive // numbers that add upto given number/* arr - array to store the combination    index - next location in array    num - given number    reducedNum - reduced number */functionfindCombinationsUtil($arr, $index,                               $num, $reducedNum){    // Base condition    if($reducedNum< 0)        return;    // If combination is     // found, print it    if($reducedNum== 0)    {        for($i= 0; $i< $index; $i++)            echo$arr[$i] , " ";        echo"\n";        return;    }    // Find the previous number     // stored in arr[] It helps     // in maintaining increasing order    $prev= ($index== 0) ? 1 : $arr[$index- 1];    // note loop starts from previous     // number i.e. at array location    // index - 1    for($k= $prev; $k<= $num; $k++)    {        // next element of array is k        $arr[$index] = $k;        // call recursively with        // reduced number        findCombinationsUtil($arr, $index+ 1,                              $num, $reducedNum- $k);    }}/* Function to find out all combinations of positive numbers that add upto given number.It uses findCombinationsUtil() */functionfindCombinations($n){    // array to store the combinations    // It can contain max n elements    $arr= array();    //find all combinations    findCombinationsUtil($arr, 0, $n, $n);}// Driver code$n= 5;findCombinations($n);// This code is contributed by ajit?> | 
Javascript
| <script>// Javascript program to find out// all combinations of positive// numbers that add upto given// number    /* arr - array to store the    combination    index - next location in array    num - given number    reducedNum - reduced number */functionfindCombinationsUtil(arr, index,                                 num, reducedNum){    // Base condition    if(reducedNum < 0)        return;     // If combination is    // found, print it    if(reducedNum == 0)    {        for(let i = 0; i < index; i++)               document.write (arr[i] + " ");           document.write("<br/>");        return;    }     // Find the previous number    // stored in arr[]. It helps    // in maintaining increasing    // order    let prev = (index == 0) ?                          1 : arr[index - 1];     // note loop starts from    // previous number i.e. at    // array location index - 1    for(let k = prev; k <= num ; k++)    {        // next element of        // array is k        arr[index] = k;         // call recursively with        // reduced number        findCombinationsUtil(arr, index + 1, num,                                 reducedNum - k);    }} /* Function to find out allcombinations of positivenumbers that add upto givennumber. It uses findCombinationsUtil() */functionfindCombinations(n){    // array to store the combinations    // It can contain max n elements    let arr = [];     // find all combinations    findCombinationsUtil(arr, 0, n, n);}// Driver Code    let n = 5;    findCombinations(n);                             </script> | 
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.
This article is contributed by Aditya Goel. If you like neveropen and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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 
Time Complexity : O(2^n)
Auxiliary Space : O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!










