Sum of squares of all Subsets of given Array Last Updated : 13 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Given an array arr[]. The value of a subset of array A is defined as the sum of squares of all the numbers in that subset. The task is to calculate the sum of values of all possible non-empty subsets of the given array. Since, the answer can be large print the val mod 1000000007.Examples: Input: arr[] = {3, 7} Output: 116 val({3}) = 32 = 9 val({7}) = 72 = 49 val({3, 7}) = 32 + 72 = 9 + 49 = 58 9 + 49 + 58 = 116Input: arr[] = {1, 1, 1} Output: 12 Naive approach: A simple approach is to find all the subset and then square each element in that subset and add it to the result. The time complexity of this approach will be O(2N)Efficient approach: It can be observed that in all the possible subsets of the given array, every element will occur 2N - 1 times where N is the size of the array. So the contribution of any element X in the sum will be 2N - 1 * X2.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 squares of subsets long long subset_square_sum(vector<int>& A) { int n = (int)A.size(); long long ans = 0; // Sqauaring the elements // and adding it to ans for (int i : A) { ans += (1LL * i * i) % mod; ans %= mod; } return (1LL * ans * power(n - 1)) % mod; } // Driver code int main() { vector<int> A = { 3, 7 }; cout << subset_square_sum(A); return 0; } Java // Java implementation of the approach class GFG { static final int mod = (int)(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 squares of subsets static long subset_square_sum(int A[]) { int n = A.length; long ans = 0; // Sqauaring the elements // and adding it to ans for (int i : A) { ans += (1 * i * i) % mod; ans %= mod; } return (1 * ans * power(n - 1)) % mod; } // Driver code public static void main (String[] args) { int A[] = { 3, 7 }; System.out.println(subset_square_sum(A)); } } // This code is contributed by AnkitRai01 Python3 # Python3 implementation of the approach mod = 10**9 + 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 # squares of subsets def subset_square_sum(A): n = len(A) ans = 0 # Squaring the elements # and adding it to ans for i in A: ans += i * i % mod ans %= mod return ans * power(n - 1) % mod # Driver code A = [3, 7] print(subset_square_sum(A)) # This code is contributed by Mohit Kumar C# // C# implementation of the approach using System; class GFG { static readonly int mod = (int)(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 squares of subsets static long subset_square_sum(int []A) { int n = A.Length; long ans = 0; // Sqauaring the elements // and adding it to ans foreach (int i in A) { ans += (1 * i * i) % mod; ans %= mod; } return (1 * ans * power(n - 1)) % mod; } // Driver code public static void Main (String[] args) { int []A = { 3, 7 }; Console.WriteLine(subset_square_sum(A)); } } // This code is contributed by 29AjayKumar JavaScript <script> // Javascript implementation of the approach const mod = 1000000000 + 7; // Function to return (2^P % mod) function power(p) { let res = 1; for (let i = 1; i <= p; ++i) { res *= 2; res %= mod; } return res % mod; } // Function to return the sum of squares of subsets function subset_square_sum(A) { let n = A.length; let ans = 0; // Sqauaring the elements // and adding it to ans for (let i = 0; i < n; i++) { ans += (A[i] * A[i]) % mod; ans %= mod; } return (ans * power(n - 1)) % mod; } // Driver code let A = [ 3, 7 ]; document.write(subset_square_sum(A)); </script> Output: 116 Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Sum of squares of all Subsets of given Array A adshin21 Follow Improve Article Tags : Misc Mathematical DSA Arrays subset +1 More Practice Tags : ArraysMathematicalMiscsubset Similar Reads Sum of cubes of all Subsets of given Array 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 = 5 min read Sum of subsets of all the subsets of an array | O(N) Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset wi 7 min read Sum of all subsets of a given size (=K) Given an array arr[] consisting of N integers and a positive integer K, the task is to find the sum of all the subsets of size K. Examples: Input: arr[] = {1, 2, 4, 5}, K = 2Output: 36Explanation:The subsets of size K(= 2) are = {1, 2}, {1, 4}, {1, 5}, {2, 4}, {2, 5}, {4, 5}. Now, the sum of all sub 7 min read Sum of subsets of all the subsets of an array | O(2^N) Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset wi 6 min read Sum of subsets of all the subsets of an array | O(3^N) Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset wi 6 min read Like