Open In App

Find the sum of maximum difference possible from all subset of a given array.

Last Updated : 19 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We are given an array arr[] of n non-negative integers (repeated elements allowed), find out the sum of maximum difference possible from all subsets of the given array. 
Suppose max(s) represents the maximum value in any subset ‘s’ whereas min(s) represents the minimum value in the set ‘s’. We need to find the sum of max(s)-min(s) for all possible subsets.

Examples: 

Input : arr[] = {1, 2, 3}
Output : result = 6

Explanation : 
All possible subset and for each subset s,
max(s)-min(s) are as :
SUBSET    |  max(s) | min(s) | max(s)-min(s)
{1, 2}    |  2      |  1     |   1
{1, 3}    |  3      |  1     |   2
{2, 3}    |  3      |  2     |   1
{1, 2, 3} |  3      |  1     |   2
Total Difference sum = 6
Note : max(s) - min(s) for all subset with 
single element must be zero.

Input : arr[] = {1, 2, 2}
Output : result = 3

Explanation : 
All possible subset and for each subset s,
max(s)-min(s) are as :
  SUBSET  |  max(s) | min(s)| max(s)-min(s)
  {1, 2}  |  2      |  1    |   1
  {1, 2}  |  2      |  1    |   1
  {2, 2}  |  2      |  2    |   0
{1, 2, 2} |  2      |  1    |   1
Total Difference sum = 3

Basic Approach: 

Compute the sum of the maximum element of each subset, and the sum of the minimum element of each subset separately, and then subtract the minimum sum from the maximum to get the answer. The sum of the maximum/ minimum element of each subset can be computed easily by iterating through the elements of each subset. But as we have to iterate through all subsets the time complexity for this approach is exponential O(n2^n).

Efficient Approach: 

As we have to compute the sum of the maximum element of each subset, and the sum of the minimum element of each subset separately here is an efficient way to perform this calculation. 

Sum of Minimum Elements of All Subsets: 

Let us say that the elements of arr[] in non-decreasing order are {a1,a2,…, an}. Now, we can partition the subsets of arr[] into the following categories: 

  • Subsets containing element a1: These subsets can be obtained by taking any subset of {a2,a3,…, an} and then adding a1 into it. The number of such subsets will be 2n-1, and they all have a1 as their minimum element.
  • Subsets not containing element a1, but containing a2: These subsets can be obtained by taking any subset of {a3, a4,…,an}, and then adding a2 into it. The number of such subsets will be 2n-2, and they all have a2 as their minimum element.
  • …..
  • Subsets not containing elements a1, a2,…, ai-1 but containing ai: These subsets can be obtained by taking any subset of {ai+1,ai+2,…, an}, and then adding ai into it. The number of such subsets will be 2n-i, and they all have ai as their minimum element.

it can be seen that the above iteration is complete, i.e., it considers each subset exactly once. Hence, the sum of the minimum element of all subsets will be:
min_sum = a1*2n-1 + a2*2n-2 + … + an*20 
This sum can be computed easily in linear time with help of the Horner method
Similarly, we can compute the sum of the maximum element of all subsets of arr[]. The only difference is that we need to iterate the elements of arr[] in non-increasing order. 

Note: We may have a large answer, so we have to calculate the answer with mod 10^9 +7.

C++





Java





Python3




# python for finding max min difference
#from all subset of given set
  
MOD = 1000000007;
  
# function for sum of max min difference 
def maxMin (arr,n):
      
    # sort all numbers
    arr.sort()
      
    # iterate over array and with help of 
    # horner's rule calc max_sum and min_sum
    min_sum = 0
    max_sum = 0
    for i in range(0,n):
          
        max_sum = 2 * max_sum + arr[n-1-i];
        max_sum %= MOD;
        min_sum = 2 * min_sum + arr[i];
        min_sum %= MOD;
      
    return (max_sum - min_sum + MOD) % MOD;
  
# Driver Code
arr = [1, 2, 3, 4]
n = len(arr)
print( maxMin(arr, n))
  
# This code is contributed by Sam007.


C#




   
// C# Code to Find the sum of maximum 
// difference possible from all subset 
// of a given array.
using System;
  
class GFG {
      
    public static int MOD = 1000000007;
      
    // function for sum of max min difference 
    public static long maxMin (int []arr, int n) 
    {
        // sort all numbers
        Array.Sort(arr);
          
        // iterate over array and with help of 
        // horner's rule calc max_sum and min_sum
        long min_sum = 0, max_sum = 0;
        for (int i = 0; i < n; i++)
        {
            max_sum = 2 * max_sum + arr[n - 1 - i];
            max_sum %= MOD;
            min_sum = 2 * min_sum + arr[i];
            min_sum %= MOD;
        }
      
        return (max_sum - min_sum + MOD) % MOD;
    }
      
    // Driver Code
    public static void Main() 
    {
            int []arr = {1, 2, 3, 4};
            int n = arr.Length;
            Console.Write(maxMin(arr, n));    
    }
}
  
// This code is contributed by nitin mittal


PHP




<?php
// PHP for finding max min difference
// from all subset of given set
$MOD = 1000000007;
  
// function for sum of 
// max min difference 
function maxMin ($arr, $n
{
    global $MOD;
      
    // sort all numbers
    sort($arr);
      
    // iterate over array 
    // and with help of 
    // horner's rule calc 
    // max_sum and min_sum
    $min_sum = 0; $max_sum = 0;
      
    for ($i = 0; $i < $n;$i++)
    {
        $max_sum = 2 * $max_sum
                       $arr[$n - 1 - $i];
        $max_sum %= $MOD;
        $min_sum = 2 * $min_sum + $arr[$i];
        $min_sum %= $MOD;
    }
  
    return ($max_sum - $min_sum + $MOD) % $MOD;
}
  
// Driver Code
$arr = array(1, 2, 3, 4);
$n = count($arr);
echo maxMin($arr, $n);
  
// This code is contributed by vt_m. 
?>


Javascript




<script>
  
// Javascript for finding max min difference
// from all subset of given set
  
var MOD = 1000000007;
  
// function for sum of max min difference 
function maxMin (arr, n) 
{
    // sort all numbers
    arr.sort((a,b)=> a-b);
      
    // iterate over array and with help of 
    // horner's rule calc max_sum and min_sum
    var min_sum = 0, max_sum = 0;
    for (var i = 0; i < n; i++)
    {
        max_sum = 2 * max_sum + arr[n-1-i];
        max_sum %= MOD;
        min_sum = 2 * min_sum + arr[i];
        min_sum %= MOD;
    }
  
    return (max_sum - min_sum + MOD) % MOD;
}
  
// Driver Code
var arr = [1, 2, 3, 4];
var n = arr.length;
document.write( maxMin(arr,n));
  
</script>


Output

23

 



Similar Reads

Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
Given an array A containing N elements. Partition any subset of this array into two disjoint subsets such that both the subsets have an identical sum. Obtain the maximum sum that can be obtained after partitioning. Note: It is not necessary to partition the entire array, that is any element might not contribute to any of the partition. Examples: In
24 min read
Split Array into K non-overlapping subset such that maximum among all subset sum is minimum
Given an array arr[] consisting of N integers and an integer K, the task is to split the given array into K non-overlapping subsets such that the maximum among the sum of all subsets is minimum. Examples: Input: arr[] = {1, 7, 9, 2, 12, 3, 3}, M = 3Output: 13Explanation:One possible way to split the array into 3 non-overlapping subsets is {arr[4],
7 min read
Sum of maximum and minimum of Kth subset ordered by increasing subset sum
Given an integer N and a set of all non-negative powers of N as S = {N0, N1, N2, N3, ... }, arrange all non-empty subsets of S in increasing order of subset-sum. The task is to find the sum of the greatest and smallest elements of the Kth subset from that ordering. Examples: Input: N = 4, K = 3Output: 5Explanation: S = {1, 4, 16, 64, ...}.Therefore
7 min read
Largest possible Subset from an Array such that no element is K times any other element in the Subset
Given an array arr[] consisting of N distinct integers and an integer K, the task is to find the maximum size of a subset possible such that no element in the subset is K times any other element of the subset(i.e. no such pair {n, m} should be present in the subset such that either m = n * K or n = m * K). Examples: Input: arr[] = {2, 8, 6, 5, 3},
7 min read
Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N
Given an array of N integers, the task is to find a non-empty subset such that the sum of elements of the subset is divisible by N. Output any such subset with its size and the indices(1-based indexing) of elements in the original array if it exists. Prerequisites: Pigeonhole PrincipleExamples: Input: arr[] = { 2, 3, 7, 1, 9 } Output: 2 1 2 The req
8 min read
Maximum Subset Sum possible by negating the entire sum after selecting the first Array element
Given an array A[] consisting of N integers, the task is to find the maximum subset-sum possible if the sum of all the elements is negated if the first element of the array is included. An empty subset(having sum 0) can also be considered. Examples: Input: N = 5, A[] = {1, 10, 4, -6, 3} Output: 17 Explanation: On excluding A[0], subset with maximum
7 min read
Maximum size of subset such that product of all subset elements is a factor of N
Given an integer N and an array arr[] having M integers, the task is to find the maximum size of the subset such that the product of all elements of the subset is a factor of N. Examples: Input: N = 12, arr[] = {2, 3, 4}Output: 2Explanation: The given array 5 subsets such that the product of all elements of the subset is a factor of 12. They are {2
6 min read
Largest value of K that a set of all possible subset-sum values of given Array contains numbers [0, K]
Given an array arr[] of N integers, the task is to find the maximum count of K, i.e, consecutive integers from 0 to K, that is present in the set S, where S contains all the possible subset-sum values of the array arr[]. Examples: Input: arr[] = {1, 3}Output: 2Explanation: The possible subsets are {}, {1}, {3}, {1, 3} and their respective sums are
6 min read
Minimum Subset sum difference problem with Subset partitioning
Given a set of N integers with up to 40 elements, the task is to partition the set into two subsets of equal size (or the closest possible), such that the difference between the sums of the subsets is minimized. If the size of the set is odd, one subset will have one more element than the other. If the size is even, both subsets will have the same
13 min read
Smallest subset of maximum sum possible by splitting array into two subsets
Given an array arr[] consisting of N integers, the task is to print the smaller of the two subsets obtained by splitting the array into two subsets such that the sum of the smaller subset is maximized. Examples: Input: arr[] = {5, 3, 2, 4, 1, 2}Output: 4 5Explanation:Split the array into two subsets as {4, 5} and {1, 2, 2, 3}.The subset {4, 5} is o
11 min read
Article Tags :
Practice Tags :