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++
#include <iostream>
using namespace std;
int sum = 0;
void Combination( int a[], int combi[], int n,
int r, int depth, int index) {
if (index == r) {
int product = 1;
for ( int i = 0; i < r; i++)
product = product * combi[i];
sum += product;
return ;
}
for ( int i = depth; i < n; i++) {
combi[index] = a[i];
Combination(a, combi, n, r, i + 1, index + 1);
}
}
void allCombination( int a[], int n) {
for ( int i = 1; i <= n; i++) {
int *combi = new int [i];
Combination(a, combi, n, i, 0, 0);
cout << "f(" << i << ") --> " << sum << "\n" ;
sum = 0;
free (combi);
}
}
int main() {
int n = 5;
int *a = new int [n];
for ( int i = 0; i < n; i++)
a[i] = i + 1;
allCombination(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int sum = 0 ;
static void Combination( int []a, int []combi,
int n, int r,
int depth, int index)
{
if (index == r)
{
int product = 1 ;
for ( int i = 0 ; i < r; i++)
product = product * combi[i];
sum += product;
return ;
}
for ( int i = depth; i < n; i++)
{
combi[index] = a[i];
Combination(a, combi, n, r,
i + 1 , index + 1 );
}
}
static void allCombination( int []a,
int n)
{
for ( int i = 1 ; i <= n; i++)
{
int []combi = new int [i];
Combination(a, combi, n,
i, 0 , 0 );
System.out.print( "f(" + i + ") --> " +
sum + "\n" );
sum = 0 ;
}
}
public static void main(String args[])
{
int n = 5 ;
int []a = new int [n];
for ( int i = 0 ; i < n; i++)
a[i] = i + 1 ;
allCombination(a, n);
}
}
|
Python3
def Combination(a, combi, n, r, depth, index):
global Sum
if index = = r:
product = 1
for i in range (r):
product = product * combi[i]
Sum + = product
return
for i in range (depth, n):
combi[index] = a[i]
Combination(a, combi, n, r,
i + 1 , index + 1 )
def allCombination(a, n):
global Sum
for i in range ( 1 , n + 1 ):
combi = [ 0 ] * i
Combination(a, combi, n, i, 0 , 0 )
print ( "f(" , i, ") --> " , Sum )
Sum = 0
Sum = 0
n = 5
a = [ 0 ] * n
for i in range (n):
a[i] = i + 1
allCombination(a, n)
|
C#
using System;
class GFG
{
static int sum = 0;
static void Combination( int []a, int []combi,
int n, int r,
int depth, int index)
{
if (index == r)
{
int product = 1;
for ( int i = 0; i < r; i++)
product = product * combi[i];
sum += product;
return ;
}
for ( int i = depth; i < n; i++)
{
combi[index] = a[i];
Combination(a, combi, n, r,
i + 1, index + 1);
}
}
static void allCombination( int []a,
int n)
{
for ( int i = 1; i <= n; i++)
{
int []combi = new int [i];
Combination(a, combi, n,
i, 0, 0);
Console.Write( "f(" + i + ") --> " +
sum + "\n" );
sum = 0;
}
}
static void Main()
{
int n = 5;
int []a = new int [n];
for ( int i = 0; i < n; i++)
a[i] = i + 1;
allCombination(a, n);
}
}
|
Javascript
<script>
let sum = 0;
function Combination(a, combi, n, r, depth, index)
{
if (index == r)
{
let product = 1;
for (let i = 0; i < r; i++)
product = product * combi[i];
sum += product;
return ;
}
for (let i = depth; i < n; i++)
{
combi[index] = a[i];
Combination(a, combi, n, r,
i + 1, index + 1);
}
}
function allCombination(a, n)
{
for (let i = 1; i <= n; i++)
{
let combi = [];
Combination(a, combi, n,
i, 0, 0);
document.write( "f(" + i + ") --> " +
sum + "<br/>" );
sum = 0;
}
}
let n = 5;
let a = [];
for (let i = 0; i < n; i++)
a[i] = i + 1;
allCombination(a, n);
</script>
|
Output
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++
#include <iostream>
using namespace std;
void postfix( int a[], int n) {
for ( int i = n - 1; i > 0; i--)
a[i - 1] = a[i - 1] + a[i];
}
void modify( int a[], int n) {
for ( int i = 1; i < n; i++)
a[i - 1] = i * a[i];
}
void allCombination( int a[], int n) {
int sum = 0;
for ( int i = 1; i <= n; i++)
sum += i;
cout << "f(1) --> " << sum << "\n" ;
for ( int i = 1; i < n; i++) {
postfix(a, n - i + 1);
sum = 0;
for ( int j = 1; j <= n - i; j++) {
sum += (j * a[j]);
}
cout << "f(" << i + 1 << ") --> " << sum << "\n" ;
modify(a, n);
}
}
int main() {
int n = 5;
int *a = new int [n];
for ( int i = 0; i < n; i++)
a[i] = i + 1;
allCombination(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void postfix( int a[], int n)
{
for ( int i = n - 1 ; i > 0 ; i--)
{
a[i - 1 ] = a[i - 1 ] + a[i];
}
}
static void modify( int a[], int n)
{
for ( int i = 1 ; i < n; i++)
{
a[i - 1 ] = i * a[i];
}
}
static void allCombination( int a[], int n)
{
int sum = 0 ;
for ( int i = 1 ; i <= n; i++)
{
sum += i;
}
System.out.println( "f(1) --> " + sum);
for ( int i = 1 ; i < n; i++)
{
postfix(a, n - i + 1 );
sum = 0 ;
for ( int j = 1 ; j <= n - i; j++)
{
sum += (j * a[j]);
}
System.out.println( "f(" + (i + 1 ) +
") --> " + sum);
modify(a, n);
}
}
public static void main(String[] args)
{
int n = 5 ;
int [] a = new int [n];
for ( int i = 0 ; i < n; i++)
{
a[i] = i + 1 ;
}
allCombination(a, n);
}
}
|
Python3
def postfix(a, n):
for i in range (n - 1 , 1 , - 1 ):
a[i - 1 ] = a[i - 1 ] + a[i]
def modify(a, n):
for i in range ( 1 , n):
a[i - 1 ] = i * a[i];
def allCombination(a, n):
sum = 0
for i in range ( 1 , n + 1 ):
sum + = i
print ( "f(1) --> " , sum )
for i in range ( 1 , n):
postfix(a, n - i + 1 )
sum = 0
for j in range ( 1 , n - i + 1 ):
sum + = (j * a[j])
print ( "f(" , i + 1 , ") --> " , sum )
modify(a, n)
if __name__ = = "__main__" :
n = 5
a = [ 0 ] * n
for i in range (n):
a[i] = i + 1
allCombination(a, n)
|
C#
using System;
class GFG
{
static void postfix( int []a, int n)
{
for ( int i = n - 1; i > 0; i--)
{
a[i - 1] = a[i - 1] + a[i];
}
}
static void modify( int []a, int n)
{
for ( int i = 1; i < n; i++)
{
a[i - 1] = i * a[i];
}
}
static void allCombination( int []a, int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
{
sum += i;
}
Console.WriteLine( "f(1) --> " + sum);
for ( int i = 1; i < n; i++)
{
postfix(a, n - i + 1);
sum = 0;
for ( int j = 1; j <= n - i; j++)
{
sum += (j * a[j]);
}
Console.WriteLine( "f(" + (i + 1) +
") --> " + sum);
modify(a, n);
}
}
public static void Main(String[] args)
{
int n = 5;
int [] a = new int [n];
for ( int i = 0; i < n; i++)
{
a[i] = i + 1;
}
allCombination(a, n);
}
}
|
Javascript
<script>
function postfix(a,n)
{
for (let i = n - 1; i > 0; i--)
{
a[i - 1] = a[i - 1] + a[i];
}
}
function modify(a,n)
{
for (let i = 1; i < n; i++)
{
a[i - 1] = i * a[i];
}
}
function allCombination(a,n)
{
let sum = 0;
for (let i = 1; i <= n; i++)
{
sum += i;
}
document.write( "f(1) --> " + sum+ "<br>" );
for (let i = 1; i < n; i++)
{
postfix(a, n - i + 1);
sum = 0;
for (let j = 1; j <= n - i; j++)
{
sum += (j * a[j]);
}
document.write( "f(" + (i + 1) +
") --> " + sum+ "<br>" );
modify(a, n);
}
}
let n = 5;
let a = new Array(n);
for (let i = 0; i < n; i++)
{
a[i] = i + 1;
}
allCombination(a, n);
</script>
|
Output
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.
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 :
20 Dec, 2022
Like Article
Save Article