Open In App

Sum of cubes of all Subsets of given Array

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array

arr[]

, the task is to calculate the sum of cubes of all possible non-empty subsets of the given array. Since, the answer can be large, print the value as

mod

1000000007.

Examples:

Input: arr[] = {1, 2} Output: 18 subset({1}) = 13 = 1 subsetval({2}) = 23 = 8 subset({1, 2}) = 13 + 23 = 1 + 8 = 9 Sum of cubes of all Subsets = 1 + 8 + 9 = 18 Input: arr[] = {1, 1, 1} Output: 12

Naive approach:

A simple approach is to

find all the subset

and then cube each element in that subset and add it to the result. The time complexity of this approach will be

O(2

N

)

Efficient approach:

  • It can be observed that each element of the original array appears in 2(N – 1) times in all subsets.
  • Therefore contribution of any element arri in the final answer will be
    arri * 2(N – 1)
  • So, the Sum of cubes of all Subsets will be
    [arr03 + arr13 + arr23 + … + arr(N-1)3] * 2(N – 1)

Below is the implementation of the above approach:

C++




// C++ implementation of the approach
 
#include <bits/stdc++.h>
using namespace std;
 
const int mod = 1e9 + 7;
 
// Function to return (2^P % mod)
long long power(int p)
{
    long long res = 1;
    for (int i = 1; i <= p; ++i) {
        res *= 2;
        res %= mod;
    }
    return res % mod;
}
 
// Function to return
// the sum of cubes of subsets
long long subset_cube_sum(vector<int>& A)
{
 
    int n = (int)A.size();
 
    long long ans = 0;
 
    // cubing the elements
    // and adding it to ans
    for (int i : A) {
        ans += (1LL * i * i * i) % mod;
        ans %= mod;
    }
 
    return (1LL * ans * power(n - 1))
           % mod;
}
 
// Driver code
int main()
{
    vector<int> A = { 1, 2 };
 
    cout << subset_cube_sum(A);
 
    return 0;
}


Java




// Java implementation of the approach
public class GFG {
    static final long MOD = (long) 1e9 + 7;
 
    // Function to return (2^P % MOD)
    static long power(int p) {
        long res = 1;
        for (int i = 1; i <= p; i++) {
            res *= 2;
            res %= MOD;
        }
        return res % MOD;
    }
 
    // Function to return the sum of cubes of subsets
    static long subsetCubeSum(int[] A) {
        int n = A.length;
        long ans = 0;
 
        // Cubing the elements and adding it to ans
        for (int i : A) {
            ans += (i * i * i) % MOD;
            ans %= MOD;
        }
 
        return (ans * power(n - 1)) % MOD;
    }
 
    // Driver code
    public static void main(String[] args) {
        int[] A = {1, 2};
        System.out.println(subsetCubeSum(A));
    }
}


Python3




# Python3 implementation of the approach
mod = int(1e9) + 7;
 
# Function to return (2^P % mod)
def power(p) :
 
    res = 1;
    for i in range(1, p + 1) :
        res *= 2;
        res %= mod;
     
    return res % mod;
 
# Function to return
# the sum of cubes of subsets
def subset_cube_sum(A) :
 
    n = len(A);
 
    ans = 0;
 
    # cubing the elements
    # and adding it to ans
    for i in A :
        ans += (i * i * i) % mod;
        ans %= mod;
 
    return (ans * power(n - 1)) % mod;
 
# Driver code
if __name__ == "__main__" :
 
    A = [ 1, 2 ];
 
    print(subset_cube_sum(A));
     
# This code is contributed by Yash_R


C#




// C# implementation for the above approach
using System;
 
public class GFG {
    static readonly long MOD = (long)1e9 + 7;
 
    // Function to return (2^P % MOD)
    static long Power(int p) {
        long res = 1;
        for (int i = 1; i <= p; i++) {
            res = (res * 2) % MOD;
        }
        return res;
    }
 
    // Function to return the sum of cubes of subsets
    static long SubsetCubeSum(int[] A) {
        int n = A.Length;
        long ans = 0;
 
        // Cubing the elements and adding it to ans
        foreach (int i in A) {
            ans = (ans + ((i * i * i) % MOD)) % MOD;
        }
 
        return (ans * Power(n - 1)) % MOD;
    }
 
    // Driver code
    public static void Main(string[] args) {
        int[] A = { 1, 2 };
        Console.WriteLine(SubsetCubeSum(A));
    }
}


Javascript




const mod = BigInt(1e9 + 7);
 
// Function to return (2^P % mod)
function power(p) {
    let res = BigInt(1);
    for (let i = 1; i <= p; ++i) {
        res *= BigInt(2);
        res %= mod;
    }
    return res;
}
 
// Function to return the sum of cubes of subsets
function subsetCubeSum(A) {
    const n = A.length;
 
    let ans = BigInt(0);
 
    // Cubing the elements and adding it to ans
    for (let i of A) {
        ans += (BigInt(i) * BigInt(i) * BigInt(i)) % mod;
        ans %= mod;
    }
 
    return (ans * power(n - 1)) % mod;
}
 
// Driver code
const A = [1, 2];
 
console.log(subsetCubeSum(A).toString());


Output

18





Time Complexity:

O(N)



Last Updated : 09 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads