Sum of XOR of all possible subsets

Last Updated : 13 Sep, 2023

Given an array arr[] of size n, we need to find the sum of all the values that come from XORing all the elements of the subsets.

Input :  arr[] = {1, 5, 6}
Output : 28
Total Subsets = 23
1 = 1
5 = 5
6 = 6
1 ^ 5 = 4
1 ^ 6 = 7
5 ^ 6 = 3
1 ^ 5 ^ 6 = 2
0(empty subset)
Now SUM of all these XORs = 1 + 5 + 6 + 4 +
7 + 3 + 2 + 0
= 28

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

A Naive approach is to take the XOR all possible combinations of array[] elements and then perform the summation of all values. Time complexity of this approach grows exponentially so it would not be better for a large value of n.

Implementation: Recursive Code for the Naive Approach

C++

 #include using namespace std; int rec(int i, int x, int arr[], int size) {     // return the current xor sum if we reach the end of     // array     if (i == size)         return x;     // first choice can be to include the i-th element in     // the subset and thus we take its xor     int choice1 = rec(i + 1, x ^ arr[i], arr, size);        // second choice can be to include the i-th element in     // the subset and thus we take its xor     int choice2 = rec(i + 1, x, arr, size);        // return sum of both the choices as we need to find the     // sum of xor of all subsets     return choice1 + choice2; } // Returns sum of XORs of all subsets int xorSum(int arr[], int size) {     return rec(0, 0, arr, size); } // Driver code int main() {     int arr[] = { 1, 5, 6 };     int size = sizeof(arr) / sizeof(arr[0]);     cout << xorSum(arr, size); }

Java

 // Java program to implement the approach class GFG {     static int rec(int i, int x, int arr[], int size)     {                  // return the current xor sum if we reach the end of         // array         if (i == size)             return x;         // first choice can be to include the i-th element         // in the subset and thus we take its xor         int choice1 = rec(i + 1, x ^ arr[i], arr, size);            // second choice can be to include the i-th element         // in the subset and thus we take its xor         int choice2 = rec(i + 1, x, arr, size);            // return sum of both the choices as we need to find         // the sum of xor of all subsets         return choice1 + choice2;     }        // Returns sum of XORs of all subsets     static int xorSum(int arr[], int size)     {         return rec(0, 0, arr, size);     }        // Driver code     public static void main(String[] args)     {         int arr[] = { 1, 5, 6 };         int size = arr.length;                    //Function call         System.out.println(xorSum(arr, size));     } }    // This code is contributed by phasing17

Python3

 # Python3 program to implement the approach def rec(i, x, arr, size):        # return the current xor sum if we reach the end of     # array     if (i == size):         return x              # first choice can be to include the i-th element in     # the subset and thus we take its xor     choice1 = rec(i + 1, x ^ arr[i], arr, size)        # second choice can be to include the i-th element in     # the subset and thus we take its xor     choice2 = rec(i + 1, x, arr, size)        # return sum of both the choices as we need to find the     # sum of xor of all subsets     return choice1 + choice2    # Returns sum of XORs of all subsets def xorSum(arr, size):     return rec(0, 0, arr, size)    # Driver code arr = [1, 5, 6] size = len(arr)    # Function call print(xorSum(arr, size))    # This code is contributed by phasing17

C#

 // C# program to implement the approach    using System;    class GFG {     static int rec(int i, int x, int[] arr, int size)     {            // return the current xor sum if we reach the end of         // array         if (i == size)             return x;         // first choice can be to include the i-th element         // in the subset and thus we take its xor         int choice1 = rec(i + 1, x ^ arr[i], arr, size);            // second choice can be to include the i-th element         // in the subset and thus we take its xor         int choice2 = rec(i + 1, x, arr, size);            // return sum of both the choices as we need to find         // the sum of xor of all subsets         return choice1 + choice2;     }        // Returns sum of XORs of all subsets     static int xorSum(int[] arr, int size)     {         return rec(0, 0, arr, size);     }        // Driver code     public static void Main(string[] args)     {         int[] arr = { 1, 5, 6 };         int size = arr.Length;            // Function call         Console.WriteLine(xorSum(arr, size));     } }    // This code is contributed by phasing17

Javascript



Output

28

Time complexity: O(2^n)
Auxiliary space: O(n)

An Efficient approach is to find the pattern with respect to the property of XOR. Now again consider the subset in binary form like:

1 = 001
5 = 101
6 = 110
1 ^ 5 = 100
1 ^ 6 = 111
5 ^ 6 = 011
1^5^6 = 010

So if we analyze all these binary numbers of the XORs, we can observe that set bit occurs at all the positions of i(0 to n-1) will exactly contribute to half of 2n. So we can easily impose these two conditions at each such position of i.

• If there is any value of arr[] that has set ith bit set, then exactly half of 2n subsets will be of the form, so they will contribute to 2n-1+i to the final sum.
• If there is no value of arr[] that ith bit set, then we can say that there will be no term in all subsets that have a ith bit set.

The proof of the above point is as follows:

Case 1:

• Lets assume there are k elements in the array with ith bit set and k is not zero.
• So, to have a subset with ith bit set in its xor, we need it to have odd number of elements with ith bit set.
• Number of ways to choose elements with ith bit not set = 2(n-k)
• Number of ways to choose elements with ith bit set = kC1 + kC3 + kC5 …. = 2(k-1)
• Total number of ways = 2(n-1)
• Thus, the contribution towards sum becomes, 2(n+i-1)

Case 2:

• f no element has ith bit set, i.e. k = 0, the contribution of ith bit towards total sum remains 0.
• Now the question boils down to check which position of element of the arr[] will be set or not. But here is some trick that we will not iterate for all elements one by one in spite of that we can simple take the OR of all such values and multiply with 2n-1

For example

Take a OR of all arr[] elements, we get
= 1 | 5 | 6
= 001 | 101 | 110
= 111

Now to find final summation, we can write it down as:-
= 1*2n-1+2 + 1*2n-1+1 + 1*2n-1+0
= 2n-1 * (1*22 + 1*21 + 1*20 )
= 2n-1 * (1112)
= 2n-1 * 7

Put n = 3,  we get
= 28

So at last for any value of n and array elements, we can simple say that the final sum will be 2n-1 times the bitwise OR of all the inputs.

C++

 // Below is C++ approach to finding the XOR_SUM #include using namespace std;    // Returns sum of XORs of all subsets int xorSum(int arr[], int n) {     int bits = 0;        // Finding bitwise OR of all elements     for (int i=0; i < n; ++i)         bits |= arr[i];        int ans = bits * pow(2, n-1);        return ans; }    // Driver code int main() {     int arr[] = {1, 5, 6};     int size = sizeof(arr) / sizeof(arr[0]);     cout << xorSum(arr, size); }

Java

 // Java approach to finding the XOR_SUM class GFG {            // Returns sum of XORs of all subsets     static int xorSum(int arr[], int n)     {                    int bits = 0;                // Finding bitwise OR of all elements         for (int i = 0; i < n; ++i)             bits |= arr[i];                int ans = bits * (int)Math.pow(2, n-1);                return ans;     }            // Driver method     public static void main(String[] args)     {                    int arr[] = {1, 5, 6};         int size = arr.length;                    System.out.print(xorSum(arr, size));     } }    // This code is contributed by Anant Agarwal.

Python3

 # Python3 approach to finding the XOR_SUM    # Returns sum of XORs of all subsets def xorSum(arr, n):        bits = 0        # Finding bitwise OR of all elements     for i in range(n):         bits |= arr[i]        ans = bits * pow(2, n-1)        return ans    # Driver Code arr = [1, 5, 6] size = len(arr) print(xorSum(arr, size))    # This code is contributed by Anant Agarwal.

C#

 // C# approach to finding the XOR_SUM using System;    class GFG {            // Returns sum of XORs of all subsets     static int xorSum(int []arr, int n)     {                    int bits = 0;                // Finding bitwise OR of all elements         for (int i = 0; i < n; ++i)             bits |= arr[i];                int ans = bits * (int)Math.Pow(2, n - 1);                return ans;     }            // Driver code     public static void Main()     {                    int []arr = {1, 5, 6};         int size = arr.Length;                    Console.Write(xorSum(arr, size));     } }    // This code is contributed by Nitin Mittal.



Javascript



Output

28

Time complexity: O(n)
Auxiliary space: O(1)

Previous
Next