
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find sum of sum of all sub-sequences in C++
Consider we have an array A with n elements. We have to find the total sum of the sum of all the subsets of the array. So if the array is like A = [5, 6, 8], then it will be like −
Subset | Sum |
---|---|
5 | 5 |
6 | 6 |
8 | 8 |
5,6 | 11 |
6,8 | 14 |
5,8 | 13 |
5,6,8 | 19 |
Total Sum | 76 |
As the array has n elements, then we have a 2n number of subsets (including empty). If we observe it clearly, then we can find that each element occurs 2n-1 times
Example
#include<iostream> #include<cmath> using namespace std; int totalSum(int arr[], int n) { int res = 0; for (int i = 0; i < n; i++) res += arr[i]; return res * pow(2, n - 1); } int main() { int arr[] = { 5, 6, 8 }; int n = sizeof(arr)/sizeof(arr[0]); cout << "Total sum of the sum of all subsets: " << totalSum(arr, n) << endl; }
Output
Total sum of the sum of all subsets: 76
- Related Articles
- Find sub-matrix with the given sum in C++
- Count all sub-sequences having product
- Find Sum of all unique subarray sum for a given array in C++
- Print all longest common sub-sequences in lexicographical order in C++
- Sum of XOR of sum of all pairs in an array in C++
- Maximum OR sum of sub-arrays of two different arrays in C++
- Program to find sum of the sum of all contiguous sublists in Python
- Find largest sum of digits in all divisors of n in C++
- Count all sub-arrays having sum divisible by k
- Count number of sub-sequences with GCD 1 in C++
- Sum of XOR of all subarrays in C++
- Maximum sum Bi-tonic Sub-sequence in C++
- C++ Program to find minimal sum of all MEX of substrings
- Find all triplets with zero sum in C++
- Print maximum sum square sub-matrix of given size in C Program.

Advertisements