Given an array of **n ** integers. The task is to find the sum of sum of each of sub-sequence of the array.

**Examples :**

Input : arr[] = { 6, 8, 5 } Output : 76 All subsequence sum are: { 6 }, sum = 6 { 8 }, sum = 8 { 5 }, sum = 5 { 6, 8 }, sum = 14 { 6, 5 }, sum = 11 { 8, 5 }, sum = 13 { 6, 8, 5 }, sum = 19 Total sum = 76. Input : arr[] = {1, 2} Output : 6

**Method 1 (brute force):**

Generate all the sub-sequence and find the sum of each sub-sequence.

**Method 2 (efficient approach):**

For an array of size n, we have 2^n sub-sequences (including empty) in total. Observe, in total 2^{n} sub-sequences, each elements occurs 2^{n-1} times.

For example, arr[] = { 5, 6, 7 }

So, sum of sum of all sub-sequence will be (sum of all the elements) * 2^{n-1}.

Below is the implementation of this approach:

## C++

`// C++ program to find sum of all sub-sequences ` `// of an array. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return sum of sum of all sub-sequence. ` `int` `sum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `ans = 0; ` ` ` ` ` `// Finding sum of the array. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `ans += arr[i]; ` ` ` ` ` `return` `ans * ` `pow` `(2, n - 1); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 6, 7, 8 }; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` ` ` `cout << sum(arr, n) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of ` `// all sub-sequences of an array. ` `import` `java.io.*; ` `import` `java.math.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Return sum of sum of all sub-sequence. ` ` ` `static` `int` `sum(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Finding sum of the array. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `ans += arr[i]; ` ` ` ` ` `return` `ans * (` `int` `)(Math.pow(` `2` `, n - ` `1` `)); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `arr[]= { ` `6` `, ` `7` `, ` `8` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(sum(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find sum of ` `# all sub-sequences of an array. ` ` ` ` ` `# Return sum of sum of all sub-sequence. ` `def` `sm(arr , n) : ` ` ` `ans ` `=` `0` ` ` ` ` `# Finding sum of the array. ` ` ` `for` `i ` `in` `range` `(` `0` `, n) : ` ` ` `ans ` `=` `ans ` `+` `arr[i] ` ` ` ` ` `return` `ans ` `*` `pow` `(` `2` `, n ` `-` `1` `) ` ` ` ` ` `# Driver Code ` `arr ` `=` `[ ` `6` `, ` `7` `, ` `8` `] ` `n` `=` `len` `(arr) ` ` ` `print` `(sm(arr, n)) ` ` ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of ` `// all sub-sequences of an array. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Return sum of sum of all sub-sequence. ` ` ` `static` `int` `sum(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` `int` `ans = 0; ` ` ` ` ` `// Finding sum of the array. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `ans += arr[i]; ` ` ` ` ` `return` `ans * (` `int` `)(Math.Pow(2, n - 1)); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr= { 6, 7, 8 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.Write(sum(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum of ` `// all sub-sequences of an array. ` ` ` `// Return sum of sum of ` `// all sub-sequence. ` `function` `sum(` `$arr` `, ` `$n` `) ` `{ ` ` ` `$ans` `= 0; ` ` ` ` ` `// Finding sum of the array. ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$ans` `+= ` `$arr` `[` `$i` `]; ` ` ` ` ` `return` `$ans` `* pow(2, ` `$n` `- 1); ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(6, 7, 8); ` `$n` `= sizeof(` `$arr` `); ` `echo` `sum(` `$arr` `, ` `$n` `) ; ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

Output:

84

This article is contributed by **Anuj Chauhan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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:

- Find all subsequences with sum equals to K
- Find all combinations of two equal sum subsequences
- Find the count of subsequences where each element is divisible by K
- Find two equal subsequences of maximum length with at least one different index
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Sum of all subsequences of length K
- Number of subsequences with zero sum
- Sum of all subsequences of an array
- Powers of two and subsequences
- Number of Subsequences with Even and Odd Sum | Set 2
- Number of Subsequences with Even and Odd Sum
- Sum of all subsequences of a number
- Count all subsequences having product less than K
- Unique subsequences of length K with given sum
- Count of 'GFG' Subsequences in the given string
- Number of subsequences of the form a^i b^j c^k
- Generating all possible Subsequences using Recursion
- Sum of width (max and min diff) of all Subsequences