Given a number n, we need to find the sum of all the elements from all possible subsets of a set formed by first n natural numbers.

**Examples :**

Input : n = 2 Output : 6 Possible subsets are {{1}, {2}, {1, 2}}. Sum of elements in subsets is 1 + 2 + 1 + 2 = 6 Input : n = 3 Output : 24 Possible subsets are {{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}} Sum of subsets is : 1 + 2 + 3 + (1 + 2) + (1 + 3) + (2 + 3) + (1 + 2 + 3)

A **simple solution** is to generate all subsets. For every subset, compute its sum and finally return overall sum.

An **efficient solution** is based on the fact that every number from 1 to n appears exactly 2^{(n-1)} times. So our required sum is (1 + 2 + 3 + ..+ n) * 2^{(n-1)}. The sum can be written as (n * (n + 1)/2) * 2^{(n-1)}

## C++

`// CPP program to find sum of all subsets ` `// of a set. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `unsigned ` `long` `long` `findSumSubsets(` `int` `n) ` `{ ` ` ` `// sum of subsets is (n * (n + 1) / 2) * ` ` ` `// pow(2, n-1) ` ` ` `return` `(n * (n + 1) / 2) * (1 << (n - 1)); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `cout << findSumSubsets(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of all subsets ` `// of a set. ` ` ` `class` `GFG { ` ` ` `static` `long` `findSumSubsets(` `int` `n) ` ` ` `{ ` ` ` `// sum of subsets is (n * (n + 1) / 2) * ` ` ` `// pow(2, n-1) ` ` ` `return` `(n * (n + ` `1` `) / ` `2` `) * (` `1` `<< (n - ` `1` `)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.print(findSumSubsets(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find ` `# sum of all subsets ` `# of a set. ` ` ` `def` `findSumSubsets( n): ` ` ` ` ` `# sum of subsets ` ` ` `# is (n * (n + 1) / 2) * ` ` ` `# pow(2, n-1) ` ` ` `return` `(n ` `*` `(n ` `+` `1` `) ` `/` `2` `) ` `*` `(` `1` `<< (n ` `-` `1` `)) ` ` ` `# Driver code ` `n ` `=` `3` `print` `(findSumSubsets(n)) ` ` ` `# This code is contributed ` `# by sunnysingh. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of all subsets ` `// of a set. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `long` `findSumSubsets(` `int` `n) ` ` ` `{ ` ` ` ` ` `// sum of subsets is (n * (n + 1) / 2) * ` ` ` `// pow(2, n-1) ` ` ` `return` `(n * (n + 1) / 2) * (1 << (n - 1)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `Console.WriteLine(findSumSubsets(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum ` `// of all subsets of a set ` ` ` `function` `findSumSubsets(` `$n` `) ` `{ ` ` ` `// sum of subsets is (n * ` ` ` `// (n + 1) / 2) * pow(2, n-1) ` ` ` `return` `(` `$n` `* (` `$n` `+ 1) / 2) * ` ` ` `(1 << (` `$n` `- 1)); ` `} ` ` ` `// Driver Code ` `$n` `= 3; ` `echo` `findSumSubsets(` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

24

This article is contributed by **Raj Kumar**. 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:

- Sum of sum of all subsets of a set formed by first N natural numbers
- Product of all Subsets of a set formed by first N natural numbers
- Divide first N natural numbers into 3 equal sum subsets
- 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)
- Sum of series formed by difference between product and sum of N natural numbers
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Sum of product of all subsets formed by only divisors of N
- Sum of first N natural numbers with all powers of 2 added twice
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of sum-series of first N Natural numbers
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Print all increasing sequences of length k from first n natural numbers
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Find all divisors of first N natural numbers
- Maximum GCD among all pairs (i, j) of first N natural numbers
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Sum of cubes of first n odd natural numbers
- Program to find sum of first n natural numbers