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
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));     } } |
18
Time Complexity:
O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!