Given an array of integer numbers, we need to find sum of maximum number of all possible subsets.

**Examples:**

Input : arr = {3, 2, 5} Output : 28 Explanation : Subsets and their maximum are, {} maximum = 0 {3} maximum = 3 {2} maximum = 2 {5} maximum = 5 {3, 2} maximum = 3 {3, 5} maximum = 5 {2, 5} maximum = 5 {3, 2, 5} maximum = 5 Sum of maximum will be, 0 + 3 + 2 + 5 + 3 + 5 + 5 + 5 = 28, which will be our answer.

A **simple solution** is to iterate through all subsets of array and finding maximum of all of them and then adding them in our answer, but this approach will lead us to exponential time complexity.

An **efficient solution** is based on one thing, how many subsets of array have a particular element as their maximum. As in above example, four subsets have 5 as their maximum, two subsets have 3 as their maximum and one subset has 2 as its maximum. The idea is to compute these frequencies corresponding to each element of array. Once we have frequencies, we can just multiply them with array values and sum them all, which will lead to our final result.

To find frequencies, first we sort the array in non-increasing order and when we are standing at a[i] we know, all element from a[i + 1] to a[N-1] are smaller than a[i], so any subset made by these element will choose a[i] as its maximum so count of such subsets corresponding to a[i] will be, 2^(N – i – 1) (total subset made by array elements from a[i + 1] to a[N]). If same procedure is applied for all elements of array, we will get our final answer as,

res = a[0]*2^(N-1) + a[1]*2^(N-2) ….. + a[i]*2^(N-i-1) + ….. + a[N-1]*2^(0)

Now if we solve above equation as it is, calculating powers of 2 will take time at each index, instead we can reform the equation similar to horner’s rule for simplification,

res = a[N] + 2*(a[N-1] + 2*(a[N-2] + 2*( …… 2*(a[2] + 2*a[1])…..))))

Total complexity of above solution will be O(N*log(N))

## C++

`// C/C++ code to find sum of maximum of all subsets of array` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Method returns sum of maximum of all subsets` `int` `sumOfMaximumOfSubsets(` `int` `arr[], ` `int` `N)` `{` ` ` `// sorting array in decreasing order` ` ` `sort(arr, arr + N, greater<` `int` `>());` ` ` ` ` `// initializing sum with first element` ` ` `int` `sum = arr[0];` ` ` `for` `(` `int` `i = 1; i < N; i++)` ` ` `{` ` ` `// calculating evaluation similar to horner's rule` ` ` `sum = 2 * sum + arr[i];` ` ` `}` ` ` ` ` `return` `sum;` `}` ` ` `// Driver code to test above methods` `int` `main()` `{` ` ` `int` `arr[] = {3, 2, 5};` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << sumOfMaximumOfSubsets(arr, N) << endl;` ` ` `return` `0;` `}` |

## Java

`import` `java.util.Arrays;` `import` `java.util.Collections;` ` ` `// Java code to find sum of ` `// maximum of all subsets of array` `class` `GFG ` `{` ` ` ` ` `// Method returns sum of maximum of all subsets` ` ` `static` `int` `sumOfMaximumOfSubsets(Integer arr[], ` `int` `N) ` ` ` `{` ` ` `// sorting array in decreasing order` ` ` `Arrays.sort(arr, Collections.reverseOrder());` ` ` ` ` `// initializing sum with first element` ` ` `int` `sum = arr[` `0` `];` ` ` `for` `(` `int` `i = ` `1` `; i < N; i++)` ` ` `{` ` ` `// calculating evaluation similar to horner's rule` ` ` `sum = ` `2` `* sum + arr[i];` ` ` `}` ` ` ` ` `return` `sum;` ` ` `}` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{` ` ` `Integer arr[] = {` `3` `, ` `2` `, ` `5` `};` ` ` `int` `N = arr.length;` ` ` `System.out.println(sumOfMaximumOfSubsets(arr, N));` ` ` `}` `}` ` ` `/* This code contributed by PrinciRaj1992 */` |

## Python3

`# Python 3 code to find sum ` `# of maximum of all subsets ` `# of array` ` ` `# Method returns sum of` `# maximum of all subsets` `def` `sumOfMaximumOfSubsets(arr, N):` ` ` ` ` `# sorting array in` ` ` `# decreasing order` ` ` `arr.sort(reverse ` `=` `True` `)` ` ` ` ` `# initializing sum ` ` ` `# with first element` ` ` `sum` `=` `arr[` `0` `]` ` ` `for` `i ` `in` `range` `(` `1` `, N):` ` ` ` ` `# calculating evaluation ` ` ` `# similar to horner's rule` ` ` `sum` `=` `2` `*` `sum` `+` `arr[i]` ` ` ` ` `return` `sum` ` ` `# Driver code ` `arr ` `=` `[` `3` `, ` `2` `, ` `5` `]` `N ` `=` `len` `(arr) ` `print` `(sumOfMaximumOfSubsets(arr, N))` ` ` `# This code is contributed ` `# by Smitha` |

## C#

`// C# code to find sum of ` `// maximum of all subsets of array ` `using` `System;` ` ` `class` `GFG ` `{` ` ` ` ` `// Method returns sum of maximum of all subsets` ` ` `static` `int` `sumOfMaximumOfSubsets(` `int` `[]arr, ` `int` `N) ` ` ` `{` ` ` `// sorting array in decreasing order` ` ` `Array.Sort(arr);` ` ` `Array.Reverse(arr);` ` ` ` ` `// initializing sum with first element` ` ` `int` `sum = arr[0];` ` ` `for` `(` `int` `i = 1; i < N; i++)` ` ` `{` ` ` `// calculating evaluation ` ` ` `// similar to horner's rule` ` ` `sum = 2 * sum + arr[i];` ` ` `}` ` ` ` ` `return` `sum;` ` ` `}` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{` ` ` `int` `[]arr = {3, 2, 5};` ` ` `int` `N = arr.Length;` ` ` `Console.WriteLine(sumOfMaximumOfSubsets(arr, N));` ` ` `}` `}` ` ` `// This code has been contributed by 29AjayKumar` |

## PHP

`<?php` `// PHP code to find sum of maximum of ` `// all subsets of array` ` ` `// Method returns sum of maximum of all subsets` `function` `sumOfMaximumOfSubsets(` `$arr` `, ` `$N` `)` `{` ` ` `// sorting array in decreasing order` ` ` `rsort(` `$arr` `);` ` ` ` ` `// initializing sum with first element` ` ` `$sum` `= ` `$arr` `[0];` ` ` `for` `( ` `$i` `= 1; ` `$i` `< ` `$N` `; ` `$i` `++)` ` ` `{` ` ` `// calculating evaluation similar ` ` ` `// to horner's rule` ` ` `$sum` `= 2 * ` `$sum` `+ ` `$arr` `[` `$i` `];` ` ` `}` ` ` ` ` `return` `$sum` `;` `}` ` ` `// Driver Code` `$arr` `= ` `array` `(3, 2, 5);` `$N` `= ` `count` `(` `$arr` `);` `echo` `sumOfMaximumOfSubsets(` `$arr` `, ` `$N` `);` ` ` `// This code is contributed by Rajput-Ji` `?>` |

**Output:**

28

This article is contributed by **Utkarsh Trivedi**. 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.