Sum of subset differences - GeeksforGeeks

28 May, 2018
Given a set S consisting of n numbers, find the sum of difference between last and first element of each subset. We find first and last element of every subset by keeping them in same order as they appear in input set S.
i.e., sumSetDiff(S) = ∑ (last(s) – first(s)),where sum goes over all subsets s of S.
Note: Elements in the subset should be in the same order as in the set S.
Examples:
S = {5, 2, 9, 6}, n = 4

Subsets are:
{5}, last(s)-first(s) = 0.
{2}, last(s)-first(s) = 0.
{9}, last(s)-first(s) = 0.
{6}, last(s)-first(s) = 0.
{5,2}, last(s)-first(s) = -3.
{5,9}, last(s)-first(s) = 4.
{5,6}, last(s)-first(s) = 1.
{2,9}, last(s)-first(s) = 7.
{2,6}, last(s)-first(s) = 4.
{9,6}, last(s)-first(s) = -3.
{5,2,9}, last(s)-first(s) = 4.
{5,2,6}, last(s)-first(s) = 1.
{5,9,6}, last(s)-first(s) = 1.
{2,9,6}, last(s)-first(s) = 4.
{5,2,9,6}, last(s)-first(s) = 1.

Output  = -3+4+1+7+4-3+4+1+1+4+1
        = 21.

A simple solution for this problem is to find the difference between the last and first element for each subset s of set S and output the sum of ll these differences. Time complexity for this approach is O(2n).
An efficient solution to solve the problem in linear time complexity.We are given a set S consisting of n numbers, and we need to compute the sum of difference between last and first element of each subset of S, i.e.,sumSetDiff(S) = ∑ (last(s) – first(s)), where sum goes over all subsets s of S.Equivalently,sumSetDiff(S) = ∑ (last(s)) – ∑ (first(s)),In other words, we can compute the sum of last element of each subset, and the sum of first element of each subset separately, and then compute their difference.
Let us say that the elements of S are {a1, a2, a3,..., an}. Note the following observation:
Subsets containing element a1 as the first element can be obtained by taking any subset of {a2, a3,..., an} and then including a1 into it. Number of such subsets will be 2n-1.Subsets containing element a2 as the first element can be obtained by taking any subset of {a3, a4,..., an} and then including a2 into it. Number of such subsets will be 2n-2.Subsets containing element ai as the first element can be obtained by taking any subset of {ai, a(i+1),..., an} and then including ai into it. Number of such subsets will be 2n-i.Therefore, the sum of first element of all subsets will be:SumF = a1.2n-1 + a2.2n-2 +...+ an.1In a similar way we can compute the sum of last element of all subsets of S (Taking at every step ai as last element instead of first element and then obtaining all the subsets).SumL = a1.1 + a2.2 +...+ an.2n-1Finally, the answer of our problem will be SumL – SumF.Implementation:C++JavaPython3C#PHPC++// A C++ program to find sum of difference between// last and first element of each subset#include<bits/stdc++.h>  // Returns the sum of first elements of all subsetsint SumF(int S[], int n){    int sum = 0;      // Compute the SumF as given in the above explanation    for (int i = 0; i < n; i++)        sum = sum + (S[i] * pow(2, n-i-1));    return sum;}  // Returns the sum of last elements of all subsetsint SumL(int S[], int n){    int sum = 0;      // Compute the SumL as given in the above explanation    for (int i = 0; i < n; i++)        sum = sum + (S[i] * pow(2, i));    return sum;}  // Returns the difference between sum of last elements of// each subset and the sum of first elements of each subsetint sumSetDiff(int S[], int n){    return SumL(S, n) - SumF(S, n);}  // Driver program to test above functionint main(){    int n = 4;    int S[] = {5, 2, 9, 6};    printf("%d\n", sumSetDiff(S, n));    return 0;}Java// A Java program to find sum of difference // between last and first element of each // subsetclass GFG {          // Returns the sum of first elements     // of all subsets    static int SumF(int S[], int n)    {        int sum = 0;          // Compute the SumF as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                 Math.pow(2, n - i - 1));        return sum;    }      // Returns the sum of last elements     // of all subsets    static int SumL(int S[], int n)    {        int sum = 0;          // Compute the SumL as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                         Math.pow(2, i));                                   return sum;    }      // Returns the difference between sum     // of last elements of each subset and     // the sum of first elements of each     // subset    static int sumSetDiff(int S[], int n)    {        return SumL(S, n) - SumF(S, n);    }      // Driver program    public static void main(String arg[])    {        int n = 4;        int S[] = { 5, 2, 9, 6 };                  System.out.println(sumSetDiff(S, n));    }}  // This code is contributed by Anant Agarwal.Python3# Python3 program to find sum of# difference between last and # first element of each subset  # Returns the sum of first# elements of all subsetsdef SumF(S, n):      sum = 0      # Compute the SumF as given    # in the above explanation    for i in range(n):        sum = sum + (S[i] * pow(2, n - i - 1))    return sum  # Returns the sum of last# elements of all subsetsdef SumL(S, n):      sum = 0      # Compute the SumL as given    # in the above explanation    for i in range(n):        sum = sum + (S[i] * pow(2, i))    return sum    # Returns the difference between sum# of last elements of each subset and# the sum of first elements of each subsetdef sumSetDiff(S, n):      return SumL(S, n) - SumF(S, n)  # Driver programn = 4S = [5, 2, 9, 6]print(sumSetDiff(S, n))  # This code is contributed by Anant Agarwal.C#// A C# program to find sum of difference // between last and first element of each // subsetusing System;class GFG {           // Returns the sum of first elements     // of all subsets    static int SumF(int []S, int n)    {        int sum = 0;           // Compute the SumF as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                 Math.Pow(2, n - i - 1));        return sum;    }       // Returns the sum of last elements     // of all subsets    static int SumL(int []S, int n)    {        int sum = 0;           // Compute the SumL as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                         Math.Pow(2, i));                                    return sum;    }       // Returns the difference between sum     // of last elements of each subset and     // the sum of first elements of each     // subset    static int sumSetDiff(int []S, int n)    {        return SumL(S, n) - SumF(S, n);    }       // Driver program    public static void Main()    {        int n = 4;        int []S = { 5, 2, 9, 6 };                   Console.Write(sumSetDiff(S, n));    }}   // This code is contributed by nitin mittal.PHP<?php// A PHP program to find sum // of difference between last // and first element of each subset  // Returns the sum of first // elements of all subsetsfunction SumF( $S, $n){    $sum = 0;      // Compute the SumF as given     // in the above explanation    for ($i = 0; $i < $n; $i++)        $sum = $sum + ($S[$i] *                pow(2, $n - $i - 1));    return $sum;}  // Returns the sum of last// elements of all subsetsfunction SumL( $S, $n){    $sum = 0;      // Compute the SumL as given    // in the above explanation    for($i = 0; $i < $n; $i++)        $sum = $sum + ($S[$i] *                     pow(2, $i));    return $sum;}  // Returns the difference between// sum of last elements of// each subset and the sum of// first elements of each subsetfunction sumSetDiff( $S, $n){    return SumL($S, $n) - SumF($S, $n);}      // Driver Code    $n = 4;    $S = array(5, 2, 9, 6);    echo sumSetDiff($S, $n);      // This code is contributed by anuj_67.?>Output:21
Time Complexity : O(n)This article is contributed by Akash Aggarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.My Personal Notes
arrow_drop_upSave
Subsets containing element a1 as the first element can be obtained by taking any subset of {a2, a3,..., an} and then including a1 into it. Number of such subsets will be 2n-1.
Subsets containing element a2 as the first element can be obtained by taking any subset of {a3, a4,..., an} and then including a2 into it. Number of such subsets will be 2n-2.
Subsets containing element ai as the first element can be obtained by taking any subset of {ai, a(i+1),..., an} and then including ai into it. Number of such subsets will be 2n-i.Therefore, the sum of first element of all subsets will be:SumF = a1.2n-1 + a2.2n-2 +...+ an.1In a similar way we can compute the sum of last element of all subsets of S (Taking at every step ai as last element instead of first element and then obtaining all the subsets).SumL = a1.1 + a2.2 +...+ an.2n-1Finally, the answer of our problem will be SumL – SumF.Implementation:C++JavaPython3C#PHPC++// A C++ program to find sum of difference between// last and first element of each subset#include<bits/stdc++.h>  // Returns the sum of first elements of all subsetsint SumF(int S[], int n){    int sum = 0;      // Compute the SumF as given in the above explanation    for (int i = 0; i < n; i++)        sum = sum + (S[i] * pow(2, n-i-1));    return sum;}  // Returns the sum of last elements of all subsetsint SumL(int S[], int n){    int sum = 0;      // Compute the SumL as given in the above explanation    for (int i = 0; i < n; i++)        sum = sum + (S[i] * pow(2, i));    return sum;}  // Returns the difference between sum of last elements of// each subset and the sum of first elements of each subsetint sumSetDiff(int S[], int n){    return SumL(S, n) - SumF(S, n);}  // Driver program to test above functionint main(){    int n = 4;    int S[] = {5, 2, 9, 6};    printf("%d\n", sumSetDiff(S, n));    return 0;}Java// A Java program to find sum of difference // between last and first element of each // subsetclass GFG {          // Returns the sum of first elements     // of all subsets    static int SumF(int S[], int n)    {        int sum = 0;          // Compute the SumF as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                 Math.pow(2, n - i - 1));        return sum;    }      // Returns the sum of last elements     // of all subsets    static int SumL(int S[], int n)    {        int sum = 0;          // Compute the SumL as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                         Math.pow(2, i));                                   return sum;    }      // Returns the difference between sum     // of last elements of each subset and     // the sum of first elements of each     // subset    static int sumSetDiff(int S[], int n)    {        return SumL(S, n) - SumF(S, n);    }      // Driver program    public static void main(String arg[])    {        int n = 4;        int S[] = { 5, 2, 9, 6 };                  System.out.println(sumSetDiff(S, n));    }}  // This code is contributed by Anant Agarwal.Python3# Python3 program to find sum of# difference between last and # first element of each subset  # Returns the sum of first# elements of all subsetsdef SumF(S, n):      sum = 0      # Compute the SumF as given    # in the above explanation    for i in range(n):        sum = sum + (S[i] * pow(2, n - i - 1))    return sum  # Returns the sum of last# elements of all subsetsdef SumL(S, n):      sum = 0      # Compute the SumL as given    # in the above explanation    for i in range(n):        sum = sum + (S[i] * pow(2, i))    return sum    # Returns the difference between sum# of last elements of each subset and# the sum of first elements of each subsetdef sumSetDiff(S, n):      return SumL(S, n) - SumF(S, n)  # Driver programn = 4S = [5, 2, 9, 6]print(sumSetDiff(S, n))  # This code is contributed by Anant Agarwal.C#// A C# program to find sum of difference // between last and first element of each // subsetusing System;class GFG {           // Returns the sum of first elements     // of all subsets    static int SumF(int []S, int n)    {        int sum = 0;           // Compute the SumF as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                 Math.Pow(2, n - i - 1));        return sum;    }       // Returns the sum of last elements     // of all subsets    static int SumL(int []S, int n)    {        int sum = 0;           // Compute the SumL as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                         Math.Pow(2, i));                                    return sum;    }       // Returns the difference between sum     // of last elements of each subset and     // the sum of first elements of each     // subset    static int sumSetDiff(int []S, int n)    {        return SumL(S, n) - SumF(S, n);    }       // Driver program    public static void Main()    {        int n = 4;        int []S = { 5, 2, 9, 6 };                   Console.Write(sumSetDiff(S, n));    }}   // This code is contributed by nitin mittal.PHP<?php// A PHP program to find sum // of difference between last // and first element of each subset  // Returns the sum of first // elements of all subsetsfunction SumF( $S, $n){    $sum = 0;      // Compute the SumF as given     // in the above explanation    for ($i = 0; $i < $n; $i++)        $sum = $sum + ($S[$i] *                pow(2, $n - $i - 1));    return $sum;}  // Returns the sum of last// elements of all subsetsfunction SumL( $S, $n){    $sum = 0;      // Compute the SumL as given    // in the above explanation    for($i = 0; $i < $n; $i++)        $sum = $sum + ($S[$i] *                     pow(2, $i));    return $sum;}  // Returns the difference between// sum of last elements of// each subset and the sum of// first elements of each subsetfunction sumSetDiff( $S, $n){    return SumL($S, $n) - SumF($S, $n);}      // Driver Code    $n = 4;    $S = array(5, 2, 9, 6);    echo sumSetDiff($S, $n);      // This code is contributed by anuj_67.?>Output:21
Time Complexity : O(n)This article is contributed by Akash Aggarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.My Personal Notes
arrow_drop_upSave
Therefore, the sum of first element of all subsets will be:SumF = a1.2n-1 + a2.2n-2 +...+ an.1
In a similar way we can compute the sum of last element of all subsets of S (Taking at every step ai as last element instead of first element and then obtaining all the subsets).SumL = a1.1 + a2.2 +...+ an.2n-1
Finally, the answer of our problem will be SumL – SumF.
Implementation:
C++
Java
Python3
C#
PHP
// A C++ program to find sum of difference between// last and first element of each subset#include<bits/stdc++.h>  // Returns the sum of first elements of all subsetsint SumF(int S[], int n){    int sum = 0;      // Compute the SumF as given in the above explanation    for (int i = 0; i < n; i++)        sum = sum + (S[i] * pow(2, n-i-1));    return sum;}  // Returns the sum of last elements of all subsetsint SumL(int S[], int n){    int sum = 0;      // Compute the SumL as given in the above explanation    for (int i = 0; i < n; i++)        sum = sum + (S[i] * pow(2, i));    return sum;}  // Returns the difference between sum of last elements of// each subset and the sum of first elements of each subsetint sumSetDiff(int S[], int n){    return SumL(S, n) - SumF(S, n);}  // Driver program to test above functionint main(){    int n = 4;    int S[] = {5, 2, 9, 6};    printf("%d\n", sumSetDiff(S, n));    return 0;}
// A Java program to find sum of difference // between last and first element of each // subsetclass GFG {          // Returns the sum of first elements     // of all subsets    static int SumF(int S[], int n)    {        int sum = 0;          // Compute the SumF as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                 Math.pow(2, n - i - 1));        return sum;    }      // Returns the sum of last elements     // of all subsets    static int SumL(int S[], int n)    {        int sum = 0;          // Compute the SumL as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                         Math.pow(2, i));                                   return sum;    }      // Returns the difference between sum     // of last elements of each subset and     // the sum of first elements of each     // subset    static int sumSetDiff(int S[], int n)    {        return SumL(S, n) - SumF(S, n);    }      // Driver program    public static void main(String arg[])    {        int n = 4;        int S[] = { 5, 2, 9, 6 };                  System.out.println(sumSetDiff(S, n));    }}  // This code is contributed by Anant Agarwal.
# Python3 program to find sum of# difference between last and # first element of each subset  # Returns the sum of first# elements of all subsetsdef SumF(S, n):      sum = 0      # Compute the SumF as given    # in the above explanation    for i in range(n):        sum = sum + (S[i] * pow(2, n - i - 1))    return sum  # Returns the sum of last# elements of all subsetsdef SumL(S, n):      sum = 0      # Compute the SumL as given    # in the above explanation    for i in range(n):        sum = sum + (S[i] * pow(2, i))    return sum    # Returns the difference between sum# of last elements of each subset and# the sum of first elements of each subsetdef sumSetDiff(S, n):      return SumL(S, n) - SumF(S, n)  # Driver programn = 4S = [5, 2, 9, 6]print(sumSetDiff(S, n))  # This code is contributed by Anant Agarwal.
// A C# program to find sum of difference // between last and first element of each // subsetusing System;class GFG {           // Returns the sum of first elements     // of all subsets    static int SumF(int []S, int n)    {        int sum = 0;           // Compute the SumF as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                 Math.Pow(2, n - i - 1));        return sum;    }       // Returns the sum of last elements     // of all subsets    static int SumL(int []S, int n)    {        int sum = 0;           // Compute the SumL as given in         // the above explanation        for (int i = 0; i < n; i++)            sum = sum + (int)(S[i] *                         Math.Pow(2, i));                                    return sum;    }       // Returns the difference between sum     // of last elements of each subset and     // the sum of first elements of each     // subset    static int sumSetDiff(int []S, int n)    {        return SumL(S, n) - SumF(S, n);    }       // Driver program    public static void Main()    {        int n = 4;        int []S = { 5, 2, 9, 6 };                   Console.Write(sumSetDiff(S, n));    }}   // This code is contributed by nitin mittal.
<?php// A PHP program to find sum // of difference between last // and first element of each subset  // Returns the sum of first // elements of all subsetsfunction SumF( $S, $n){    $sum = 0;      // Compute the SumF as given     // in the above explanation    for ($i = 0; $i < $n; $i++)        $sum = $sum + ($S[$i] *                pow(2, $n - $i - 1));    return $sum;}  // Returns the sum of last// elements of all subsetsfunction SumL( $S, $n){    $sum = 0;      // Compute the SumL as given    // in the above explanation    for($i = 0; $i < $n; $i++)        $sum = $sum + ($S[$i] *                     pow(2, $i));    return $sum;}  // Returns the difference between// sum of last elements of// each subset and the sum of// first elements of each subsetfunction sumSetDiff( $S, $n){    return SumL($S, $n) - SumF($S, $n);}      // Driver Code    $n = 4;    $S = array(5, 2, 9, 6);    echo sumSetDiff($S, $n);      // This code is contributed by anuj_67.?>
21

Time Complexity : O(n)
This article is contributed by Akash Aggarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
nitin mittal
vt_m
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Linked List vs Array
Python | Using 2D arrays/lists the right way
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Maximum and minimum of an array using minimum number of comparisons
Queue | Set 1 (Introduction and Array Implementation)