Given an array **arr[]** of **N** non-negative integers and an integer **1 ≤ K ≤ N**. The task is to find the sum of the products of all possible subsets of **arr[]** of size **K**.

**Examples:**

Input:arr[] = {1, 2, 3, 4}, K = 2

Output:35

(1 * 2) + (1 * 3) + (1 * 4) + (2 * 3) + (2 * 4)

+ (3 * 4) = 2 + 3 + 4 + 6 + 8 + 12 = 35

Input:arr[] = {1, 2, 3, 4}, K = 3

Output:50

**Naive approach:** Generate all possible subsets of size **K** and find the resultant product of each subset. Then sum the product obtained for each subset. The time complexity of this solution would be exponential.

**Efficient approach:** Take the example of an array **a[] = {1, 2, 3}** and **K = 3**. Then,

k = 1, answer = 1 + 2 + 3 = 6

k = 2, answer = 1 * (2 + 3) + 2 * 3 + 0 = 11

k = 3, answer = 1 * (2 * 3 + 0) + 0 + 0 = 6

In the example, if the contribution of **1** is needed to be obtained in the answer for **K = 2** then the sum of all elements after the index of element **1** is required in the previously computed values for **K = 1**. It can be seen that the sum of elements **2** and **3** is required. Thus, for any **K**, the answer obtained for **K – 1** is required.

So, bottom up dynamic programming approach can be used to solve this problem. Create a table **dp[][]** and fill it in bottom up manner where **dp[i][j]** will store the contribution of an element **arr[j – 1]** to the answer for **K = i**. Hence, the recurrence relation will be,

dp[i][j] = arr[j-1] * dp[i-1][k]

answer[k] = dp[k][i]

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the sum of products of ` `// all the possible k size subsets ` `int` `sumOfProduct(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Initialising all the values to 0 ` ` ` `int` `dp[n + 1][n + 1] = { 0 }; ` ` ` ` ` `// To store the answer for ` ` ` `// current value of k ` ` ` `int` `cur_sum = 0; ` ` ` ` ` `// For k = 1, the answer will simply ` ` ` `// be the sum of all the elements ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `dp[1][i] = arr[i - 1]; ` ` ` `cur_sum += arr[i - 1]; ` ` ` `} ` ` ` ` ` `// Filling the table in bottom up manner ` ` ` `for` `(` `int` `i = 2; i <= k; i++) { ` ` ` ` ` `// To store the elements of the current ` ` ` `// row so that we will be able to use this sum ` ` ` `// for subsequent values of k ` ` ` `int` `temp_sum = 0; ` ` ` ` ` `for` `(` `int` `j = 1; j <= n; j++) { ` ` ` ` ` `// We will subtract previously computed value ` ` ` `// so as to get the sum of elements from j + 1 ` ` ` `// to n in the (i - 1)th row ` ` ` `cur_sum -= dp[i - 1][j]; ` ` ` ` ` `dp[i][j] = arr[j - 1] * cur_sum; ` ` ` `temp_sum += dp[i][j]; ` ` ` `} ` ` ` `cur_sum = temp_sum; ` ` ` `} ` ` ` `return` `cur_sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 4 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` `int` `k = 2; ` ` ` ` ` `cout << sumOfProduct(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to return the sum of products of ` `# all the possible k size subsets ` `def` `sumOfProduct(arr, n, k): ` ` ` ` ` `# Initialising all the values to 0 ` ` ` `dp ` `=` `[ [ ` `0` `for` `x ` `in` `range` `(n ` `+` `1` `)] ` `for` `y ` `in` `range` `(n ` `+` `1` `)] ` ` ` ` ` `# To store the answer for ` ` ` `# current value of k ` ` ` `cur_sum ` `=` `0` ` ` ` ` `# For k = 1, the answer will simply ` ` ` `# be the sum of all the elements ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `dp[` `1` `][i] ` `=` `arr[i ` `-` `1` `] ` ` ` `cur_sum ` `+` `=` `arr[i ` `-` `1` `] ` ` ` ` ` `# Filling the table in bottom up manner ` ` ` `for` `i ` `in` `range` `(` `2` `, k ` `+` `1` `): ` ` ` ` ` `# To store the elements of the current ` ` ` `# row so that we will be able to use this sum ` ` ` `# for subsequent values of k ` ` ` `temp_sum ` `=` `0` ` ` ` ` `for` `j ` `in` `range` `( ` `1` `, n ` `+` `1` `): ` ` ` ` ` `# We will subtract previously computed value ` ` ` `# so as to get the sum of elements from j + 1 ` ` ` `# to n in the (i - 1)th row ` ` ` `cur_sum ` `-` `=` `dp[i ` `-` `1` `][j] ` ` ` ` ` `dp[i][j] ` `=` `arr[j ` `-` `1` `] ` `*` `cur_sum ` ` ` `temp_sum ` `+` `=` `dp[i][j] ` ` ` `cur_sum ` `=` `temp_sum ` ` ` `return` `cur_sum ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `4` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `k ` `=` `2` ` ` `print` `(sumOfProduct(arr, n, k)) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Time Complexity:** O(N^{2})

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Sum of values of all possible non-empty subsets of the given array
- Sum of bitwise OR of all possible subsets of given set
- Product of values of all possible non-empty subsets of given Array
- Check if it is possible to split given Array into K odd-sum subsets
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Sum of the sums of all possible subsets
- Sum of products of all combination taken (1 to n) at a time
- Perfect Sum Problem (Print all subsets with given sum)
- Maximize sum of pairwise products generated from the given Arrays
- Sum of squares of all Subsets of given Array
- Sum of cubes of all Subsets of given Array
- Minimum difference between max and min of all K-size subsets
- Product of all sorted subsets of size K using elements whose index divide K completely
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Count of all possible pairs of disjoint subsets of integers from 1 to N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.