Sum of maximum elements of all subsets
Last Updated :
08 Oct, 2023
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.
Brute force method:
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.
Approach:
We first define an array arr containing the input integers and find its size n. We also initialize a variable sum to 0, which we will use to store the sum of maximums of all subsets.
Next, we use a nested loop. The outer loop runs from 0 to (1 << n) – 1, where (1 << n) is the number of possible subsets of an array of size n. The inner loop runs from 0 to n-1, checking if the jth element of the array is present in the current subset or not. This is done by checking if the jth bit of the binary representation of i is set or not. If it is set, we update the maximum number of the subset.
After finding the maximum number of each subset, we add it to the sum variable. Finally, we print the sum as the output.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfMaximumOfSubsets( int arr[], int n)
{
int sum=0;
for ( int i = 0; i < (1 << n); i++) {
int max_num = 0;
for ( int j = 0; j < n; j++) {
if (i & (1 << j)) {
max_num = max(max_num, arr[j]);
}
}
sum += max_num;
}
return sum;
}
int main() {
int arr[] = {3,2, 5};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << sumOfMaximumOfSubsets(arr, n) << endl;
return 0;
}
|
Java
public class GFG {
static int sumOfMaximumOfSubsets( int [] arr, int n) {
int sum = 0 ;
for ( int i = 0 ; i < ( 1 << n); i++) {
int max_num = 0 ;
for ( int j = 0 ; j < n; j++) {
if ((i & ( 1 << j)) != 0 ) {
max_num = Math.max(max_num, arr[j]);
}
}
sum += max_num;
}
return sum;
}
public static void main(String[] args) {
int [] arr = { 3 , 2 , 5 };
int n = arr.length;
System.out.println(sumOfMaximumOfSubsets(arr, n));
}
}
|
Python
def sum_of_maximum_of_subsets(arr):
n = len (arr)
total_sum = 0
for i in range ( 1 << n):
max_num = 0
for j in range (n):
if i & ( 1 << j):
max_num = max (max_num, arr[j])
total_sum + = max_num
return total_sum
def main():
arr = [ 3 , 2 , 5 ]
result = sum_of_maximum_of_subsets(arr)
print (result)
if __name__ = = "__main__" :
main()
|
C#
using System;
public class GFG
{
public static int SumOfMaximumOfSubsets( int [] arr, int n)
{
int sum = 0;
for ( int i = 0; i < (1 << n); i++)
{
int maxNum = 0;
for ( int j = 0; j < n; j++)
{
if ((i & (1 << j)) != 0)
{
maxNum = Math.Max(maxNum, arr[j]);
}
}
sum += maxNum;
}
return sum;
}
public static void Main()
{
int [] arr = { 3, 2, 5 };
int n = arr.Length;
Console.WriteLine(SumOfMaximumOfSubsets(arr, n));
}
}
|
Javascript
function sumOfMaximumOfSubsets(arr, n)
{
let sum=0;
for (let i = 0; i < (1 << n); i++) {
let max_num = 0;
for (let j = 0; j < n; j++) {
if (i & (1 << j)) {
max_num = Math.max(max_num, arr[j]);
}
}
sum += max_num;
}
return sum;
}
let arr = [3,2, 5];
let n = arr.length;
console.log(sumOfMaximumOfSubsets(arr, n));
|
output:
28
Time Complexity: O(2^n * n), where n is the size of the input array.
Auxiliary Space: O(1), since no extra space being used.
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))
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfMaximumOfSubsets( int arr[], int N)
{
sort(arr, arr + N, greater< int >());
int sum = arr[0];
for ( int i = 1; i < N; i++)
{
sum = 2 * sum + arr[i];
}
return sum;
}
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;
class GFG
{
static int sumOfMaximumOfSubsets(Integer arr[], int N)
{
Arrays.sort(arr, Collections.reverseOrder());
int sum = arr[ 0 ];
for ( int i = 1 ; i < N; i++)
{
sum = 2 * sum + arr[i];
}
return sum;
}
public static void main(String[] args)
{
Integer arr[] = { 3 , 2 , 5 };
int N = arr.length;
System.out.println(sumOfMaximumOfSubsets(arr, N));
}
}
|
Python3
def sumOfMaximumOfSubsets(arr, N):
arr.sort(reverse = True )
sum = arr[ 0 ]
for i in range ( 1 , N):
sum = 2 * sum + arr[i]
return sum
arr = [ 3 , 2 , 5 ]
N = len (arr)
print (sumOfMaximumOfSubsets(arr, N))
|
C#
using System;
class GFG
{
static int sumOfMaximumOfSubsets( int []arr, int N)
{
Array.Sort(arr);
Array.Reverse(arr);
int sum = arr[0];
for ( int i = 1; i < N; i++)
{
sum = 2 * sum + arr[i];
}
return sum;
}
public static void Main(String[] args)
{
int []arr = {3, 2, 5};
int N = arr.Length;
Console.WriteLine(sumOfMaximumOfSubsets(arr, N));
}
}
|
Javascript
<script>
function sumOfMaximumOfSubsets(arr,N)
{
arr.sort((a,b)=>a-b);
arr.reverse();
let sum = arr[0];
for (let i = 1; i < N; i++)
{
sum = 2 * sum + arr[i];
}
return sum;
}
let arr= [3, 2, 5];
let N = arr.length;
document.write(sumOfMaximumOfSubsets(arr, N));
</script>
|
PHP
<?php
function sumOfMaximumOfSubsets( $arr , $N )
{
rsort( $arr );
$sum = $arr [0];
for ( $i = 1; $i < $N ; $i ++)
{
$sum = 2 * $sum + $arr [ $i ];
}
return $sum ;
}
$arr = array (3, 2, 5);
$N = count ( $arr );
echo sumOfMaximumOfSubsets( $arr , $N );
?>
|
Time Complexity: O(N log N), since sort() function is used.
Auxiliary Space: O(1), since no extra space being used.
Share your thoughts in the comments
Please Login to comment...