Sum of products of all combination taken (1 to n) at a time

Given N, we have to find the sum of products of all combinations taken 1 to N at a time. In simple words, we have to find the sum of products of all combinations taken 1 at a time, then 2 at a time, then 3 at a time till N at a time.Â
If you think closely about the problem, a large value of N could result in producing many combinations.
Examples:Â
Input : N = 3
Output : f(1) = 6
f(2) = 11
f(3) = 6
Explanation: f(x) is sum of products of all
combination taken x at a time
1 + 2 + 3 = 6
f(2) = (1*2) + (1*3) + (2*3) = 11
f(3) = (1*2*3)
Input : N = 4
Output : f(1) = 10
f(2) = 35
f(3) = 50
f(4) = 24
Explanation: f(1) = 1 + 2 + 3 + 4 = 10
f(2) = (1*2) + (1*3) + (1*4) +
(2*3) + (2*4) + (3*4)
= 35
f(3) = (1*2*3) + (1*2*4) +(1*3*4) +
(2*3*4)
= 50
f(4) = (1*2*3*4) = 24
A Brute force approach would be to produce all the combinations and then find their products and sum.
Recursion would do the trick to produce the combinations taken x at a time.Â
Example: N = 4 taken 3 at a timeÂ
Â
C++
// Program to find SOP of all combination taken// (1 to N) at a time using brute force#include <iostream>using namespace std;Â
// to store sum of every combinationint sum = 0;Â
void Combination(int a[], int combi[], int n, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int r, int depth, int index) {Â
  // if we have reached sufficient depth  if (index == r) {         // find the product of combination    int product = 1;    for (int i = 0; i < r; i++)      product = product * combi[i];Â
    // add the product into sum    sum += product;    return;  }Â
  // recursion to produce different combination  for (int i = depth; i < n; i++) {    combi[index] = a[i];    Combination(a, combi, n, r, i + 1, index + 1);  }}Â
// function to print sum of products of// all combination taken 1-N at a timevoid allCombination(int a[], int n) {Â Â for (int i = 1; i <= n; i++) {Â
    // creating temporary array for storing    // combination    int *combi = new int[i];Â
    // call combination with r = i    // for combination taken i at a time    Combination(a, combi, n, i, 0, 0);Â
    // displaying sum    cout << "f(" << i << ") --> " << sum << "\n";    sum = 0;Â
    // free from heap area    free(combi);  }}Â
// Driver's codeint main() {Â Â int n = 5;Â Â int *a = new int[n];Â
  // storing numbers from 1-N in array  for (int i = 0; i < n; i++)    a[i] = i + 1;Â
  // calling allCombination  allCombination(a, n);Â
  return 0;} |
Java
// Program to find SOP of // all combination taken// (1 to N) at a time using// brute forceimport java.io.*;Â
class GFG{    // to store sum of    // every combination    static int sum = 0;         static void Combination(int []a, int []combi,                             int n, int r,                             int depth, int index)     {         // if we have reached    // sufficient depth    if (index == r)     {                 // find the product        // of combination        int product = 1;        for (int i = 0; i < r; i++)        product = product * combi[i];             // add the product into sum        sum += product;        return;    }         // recursion to produce     // different combination    for (int i = depth; i < n; i++)     {        combi[index] = a[i];        Combination(a, combi, n, r,                     i + 1, index + 1);    }    }         // function to print sum of     // products of all combination    // taken 1-N at a time    static void allCombination(int []a,                                int n)    {        for (int i = 1; i <= n; i++)         {                     // creating temporary array             // for storing combination            int []combi = new int[i];                     // call combination with             // r = i for combination             // taken i at a time            Combination(a, combi, n,                        i, 0, 0);                     // displaying sum            System.out.print("f(" + i + ") --> " +                                       sum + "\n");            sum = 0;        }    }         // Driver code    public static void main(String args[])     {        int n = 5;        int []a = new int[n];                 // storing numbers         // from 1-N in array        for (int i = 0; i < n; i++)            a[i] = i + 1;                 // calling allCombination        allCombination(a, n);    }} Â
// This code is contributed by // Manish Shaw(manishshaw1) |
Python3
# Python3 Program to find SOP of all combination # taken (1 to N) at a time using brute force Â
# to store sum of every combination def Combination(a, combi, n, r, depth, index):    global Sum         # if we have reached sufficient depth     if index == r:              # find the product of combination         product = 1        for i in range(r):            product = product * combi[i]              # add the product into sum         Sum += product         returnÂ
    # recursion to produce different    # combination     for i in range(depth, n):        combi[index] = a[i]         Combination(a, combi, n, r,                     i + 1, index + 1)Â
# function to print sum of products of # all combination taken 1-N at a time def allCombination(a, n):    global Sum    for i in range(1, n + 1):                 # creating temporary array for         # storing combination         combi = [0] * i              # call combination with r = i         # for combination taken i at a time         Combination(a, combi, n, i, 0, 0)              # displaying sum         print("f(", i, ") --> ", Sum)         Sum = 0Â
# Driver Code Sum = 0n = 5a = [0] * n Â
# storing numbers from 1-N in arrayfor i in range(n):Â Â Â Â a[i] = i + 1Â
# calling allCombination allCombination(a, n) Â
# This code is contributed by PranchalK |
C#
// Program to find SOP of // all combination taken// (1 to N) at a time using// brute forceusing System;Â
class GFG{    // to store sum of    // every combination    static int sum = 0;         static void Combination(int []a, int []combi,                             int n, int r,                             int depth, int index)     {         // if we have reached    // sufficient depth    if (index == r)     {                 // find the product        // of combination        int product = 1;        for (int i = 0; i < r; i++)        product = product * combi[i];             // add the product into sum        sum += product;        return;    }         // recursion to produce     // different combination    for (int i = depth; i < n; i++)     {        combi[index] = a[i];        Combination(a, combi, n, r,                     i + 1, index + 1);    }    }         // function to print sum of     // products of all combination    // taken 1-N at a time    static void allCombination(int []a,                                int n)    {    for (int i = 1; i <= n; i++)     {             // creating temporary array         // for storing combination        int []combi = new int[i];             // call combination with         // r = i for combination         // taken i at a time        Combination(a, combi, n,                    i, 0, 0);             // displaying sum        Console.Write("f(" + i + ") --> " +                                sum + "\n");        sum = 0;    }    }         // Driver code    static void Main()     {        int n = 5;        int []a = new int[n];                 // storing numbers         // from 1-N in array        for (int i = 0; i < n; i++)            a[i] = i + 1;                 // calling allCombination        allCombination(a, n);    }}Â
// This code is contributed by // Manish Shaw(manishshaw1) |
Javascript
<script>Â
// JavaScript program to find sum of all combination taken// (1 to N) at a time using brute force      // to store sum of    // every combination    let sum = 0;          function Combination(a, combi, n, r, depth, index)    {          // if we have reached    // sufficient depth    if (index == r)    {                  // find the product        // of combination        let product = 1;        for (let i = 0; i < r; i++)        product = product * combi[i];              // add the product into sum        sum += product;        return;    }          // recursion to produce    // different combination    for (let i = depth; i < n; i++)    {        combi[index] = a[i];        Combination(a, combi, n, r,                    i + 1, index + 1);    }    }          // function to print sum of    // products of all combination    // taken 1-N at a time    function allCombination(a, n)    {        for (let i = 1; i <= n; i++)        {                      // creating temporary array            // for storing combination            let combi = [];                      // call combination with            // r = i for combination            // taken i at a time            Combination(a, combi, n,                        i, 0, 0);                      // displaying sum            document.write("f(" + i + ") --> " +                                      sum + "<br/>");            sum = 0;        }    }         // Driver codeÂ
        let n = 5;        let a = [];                  // storing numbers        // from 1-N in array        for (let i = 0; i < n; i++)            a[i] = i + 1;                  // calling allCombination        allCombination(a, n);Â
</script> |
f(1) --> 15 f(2) --> 85 f(3) --> 225 f(4) --> 274 f(5) --> 120
Time complexity of the above code is exponential when the value of N is large.Â
An Efficient Method is to use the concept of dynamic programming. We don’t have to find the sum of products every time. We can make use of previous results.Â
Let’s take an example: N = 4Â
Â
C++
// CPP Program to find sum of all combination taken// (1 to N) at a time using dynamic programming#include <iostream>using namespace std;Â
// find the postfix sum arrayvoid postfix(int a[], int n) {Â Â for (int i = n - 1; i > 0; i--)Â Â Â Â a[i - 1] = a[i - 1] + a[i];}Â
// modify the array such that we don't have to// compute the products which are obtained beforevoid modify(int a[], int n) {Â Â for (int i = 1; i < n; i++)Â Â Â Â a[i - 1] = i * a[i];}Â
// finding sum of all combination taken 1 to N at a timevoid allCombination(int a[], int n) {Â
  int sum = 0;Â
  // sum taken 1 at time is simply sum of 1 - N  for (int i = 1; i <= n; i++)    sum += i;  cout << "f(1) --> " << sum << "\n";Â
  // for sum of products for all combination  for (int i = 1; i < n; i++) {Â
    // finding postfix array    postfix(a, n - i + 1);         // sum of products taken i+1 at a time    sum = 0;    for (int j = 1; j <= n - i; j++) {      sum += (j * a[j]);    }    cout << "f(" << i + 1 << ") --> " << sum << "\n";Â
    // modify the array for overlapping problem    modify(a, n);  }}Â
// Driver's Codeint main() {Â Â int n = 5;Â Â int *a = new int[n];Â
  // storing numbers from 1 to N  for (int i = 0; i < n; i++)    a[i] = i + 1;Â
  // calling allCombination  allCombination(a, n);Â
  return 0;} |
Java
// Java Program to find sum of all combination taken// (1 to N) at a time using dynamic programmingimport java.util.*;Â
class GFG {Â
    // find the postfix sum array    static void postfix(int a[], int n)     {        for (int i = n - 1; i > 0; i--)         {            a[i - 1] = a[i - 1] + a[i];        }    }Â
    // modify the array such that we don't     // have to compute the products which    // are obtained before    static void modify(int a[], int n)    {        for (int i = 1; i < n; i++)         {            a[i - 1] = i * a[i];        }    }Â
    // finding sum of all combination     // taken 1 to N at a time    static void allCombination(int a[], int n)     {        int sum = 0;Â
        // sum taken 1 at time is simply sum of 1 - N        for (int i = 1; i <= n; i++)         {            sum += i;        }        System.out.println("f(1) --> " + sum);Â
        // for sum of products for all combination        for (int i = 1; i < n; i++)         {Â
            // finding postfix array            postfix(a, n - i + 1);Â
            // sum of products taken i+1 at a time            sum = 0;            for (int j = 1; j <= n - i; j++)             {                sum += (j * a[j]);            }            System.out.println("f(" + (i + 1) +                                ") --> " + sum);Â
            // modify the array for overlapping problem            modify(a, n);        }    }Â
    // Driver's Code    public static void main(String[] args)     {        int n = 5;        int[] a = new int[n];Â
        // storing numbers from 1 to N        for (int i = 0; i < n; i++)         {            a[i] = i + 1;        }Â
        // calling allCombination        allCombination(a, n);    }}Â
// This code is contributed by 29AjayKumar |
Python3
# Python3 Program to find # sum of all combination taken# (1 to N) at a time using # dynamic programmingÂ
# Find the postfix sum arraydef postfix(a, n):Â Â Â Â Â Â Â for i in range (n - 1, 1, -1):Â Â Â Â Â Â Â Â a[i - 1] = a[i - 1] + a[i]Â
# Modify the array such # that we don't have to# compute the products # which are obtained beforedef modify(a, n):Â Â Â Â Â Â Â for i in range (1, n):Â Â Â Â Â Â Â Â a[i - 1] = i * a[i];Â
# Finding sum of all combination # taken 1 to N at a timedef allCombination(a, n):Â
    sum = 0Â
    # sum taken 1 at time is     # simply sum of 1 - N    for i in range (1, n + 1):               sum += i    print ("f(1) --> ", sum )Â
    # for sum of products for     # all combination    for i in range (1, n):Â
        # finding postfix array        postfix(a, n - i + 1)             # sum of products taken         # i+1 at a time        sum = 0                 for j in range(1, n - i + 1):            sum += (j * a[j])             print ("f(", i + 1, ") --> ", sum)Â
        # modify the array for         # overlapping problem        modify(a, n)Â
# Driver's Codeif __name__ == "__main__":Â
    n = 5    a = [0] * nÂ
    # storing numbers     # from 1 to N    for i in range(n):        a[i] = i + 1Â
    # calling allCombination    allCombination(a, n)Â
# This code is contributed by Chitranayal |
C#
// C# Program to find sum of all combination taken// (1 to N) at a time using dynamic programmingusing System;Â Â Â Â Â class GFG {Â
    // find the postfix sum array    static void postfix(int []a, int n)     {        for (int i = n - 1; i > 0; i--)         {            a[i - 1] = a[i - 1] + a[i];        }    }Â
    // modify the array such that we don't     // have to compute the products which    // are obtained before    static void modify(int []a, int n)    {        for (int i = 1; i < n; i++)         {            a[i - 1] = i * a[i];        }    }Â
    // finding sum of all combination     // taken 1 to N at a time    static void allCombination(int []a, int n)     {        int sum = 0;Â
        // sum taken 1 at time is simply sum of 1 - N        for (int i = 1; i <= n; i++)         {            sum += i;        }        Console.WriteLine("f(1) --> " + sum);Â
        // for sum of products for all combination        for (int i = 1; i < n; i++)         {Â
            // finding postfix array            postfix(a, n - i + 1);Â
            // sum of products taken i+1 at a time            sum = 0;            for (int j = 1; j <= n - i; j++)             {                sum += (j * a[j]);            }            Console.WriteLine("f(" + (i + 1) +                             ") --> " + sum);Â
            // modify the array for overlapping problem            modify(a, n);        }    }Â
    // Driver's Code    public static void Main(String[] args)     {        int n = 5;        int[] a = new int[n];Â
        // storing numbers from 1 to N        for (int i = 0; i < n; i++)         {            a[i] = i + 1;        }Â
        // calling allCombination        allCombination(a, n);    }}Â
// This code is contributed by Rajput-Ji |
Javascript
<script>// Javascript Program to find sum of all combination taken// (1 to N) at a time using dynamic programming         // find the postfix sum array    function postfix(a,n)    {        for (let i = n - 1; i > 0; i--)        {            a[i - 1] = a[i - 1] + a[i];        }    }         // modify the array such that we don't    // have to compute the products which    // are obtained before    function modify(a,n)    {        for (let i = 1; i < n; i++)        {            a[i - 1] = i * a[i];        }    }         // finding sum of all combination    // taken 1 to N at a time    function allCombination(a,n)    {        let sum = 0;          // sum taken 1 at time is simply sum of 1 - N        for (let i = 1; i <= n; i++)        {            sum += i;        }        document.write("f(1) --> " + sum+"<br>");          // for sum of products for all combination        for (let i = 1; i < n; i++)        {              // finding postfix array            postfix(a, n - i + 1);              // sum of products taken i+1 at a time            sum = 0;            for (let j = 1; j <= n - i; j++)            {                sum += (j * a[j]);            }            document.write("f(" + (i + 1) +                               ") --> " + sum+"<br>");              // modify the array for overlapping problem            modify(a, n);        }    }         // Driver's Code    let n = 5;    let a = new Array(n);    // storing numbers from 1 to N    for (let i = 0; i < n; i++)    {        a[i] = i + 1;    }      // calling allCombination    allCombination(a, n);         // This code is contributed by avanitrachhadiya2155</script> |
f(1) --> 15 f(2) --> 85 f(3) --> 225 f(4) --> 274 f(5) --> 120
Time Complexity: O(n^2)
Auxiliary Space: O(1)
You can also find the execution time of both the method for a large value of N and can see the difference for yourself.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




