Sum of XOR of all possible subsets

07 Jul, 2022
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++
Java
Python3
C#
Javascript
#include <bits/stdc++.h>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 subsetsint xorSum(int arr[], int size){    return rec(0, 0, arr, size);}// Driver codeint main(){    int arr[] = { 1, 5, 6 };    int size = sizeof(arr) / sizeof(arr[0]);    cout << xorSum(arr, size);}
// Java program to implement the approachclass 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 program to implement the approachdef 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 subsetsdef xorSum(arr, size):    return rec(0, 0, arr, size) # Driver codearr = [1, 5, 6]size = len(arr) # Function callprint(xorSum(arr, size)) # This code is contributed by phasing17
// 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
<script>     function 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        let 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        let 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    function xorSum(arr, size) {        return rec(0, 0, arr, size);    }     // Driver code    let arr = [ 1, 5, 6 ];    let size = arr.length;    document.write(xorSum(arr, size)); // This code is contributed by AshokJaiswal.</script>
28
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++
Java
Python3
C#
PHP
Javascript
// Below is C++ approach to finding the XOR_SUM#include<bits/stdc++.h>using namespace std; // Returns sum of XORs of all subsetsint 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 codeint main(){    int arr[] = {1, 5, 6};    int size = sizeof(arr) / sizeof(arr[0]);    cout << xorSum(arr, size);}
// Java approach to finding the XOR_SUMclass 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 approach to finding the XOR_SUM # Returns sum of XORs of all subsetsdef 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 Codearr = [1, 5, 6]size = len(arr)print(xorSum(arr, size)) # This code is contributed by Anant Agarwal.
// C# approach to finding the XOR_SUMusing 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.
<?php// PHP program to finding the XOR_SUM// Returns sum of XORs of all subsets function xorSum($arr, $n){    $bits = 0;     // Finding bitwise OR    // of all elements    for ($i = 0; $i < $n; ++$i)        $bits |= $arr[$i];     $ans = $bits * pow(2, $n - 1);     return $ans;}     // Driver code    $arr = array(1, 5, 6);    $size = sizeof($arr);    echo xorSum($arr, $size); // This code is contributed by nitin mittal.?>
<script>// Below is JavaScript approach to finding the XOR_SUM // Returns sum of XORs of all subsetsfunction xorSum(arr, n){    let bits = 0;     // Finding bitwise OR of all elements    for (let i=0; i < n; ++i)        bits |= arr[i];     let ans = bits * Math.pow(2, n-1);     return ans;} // Driver code     let arr = [1, 5, 6];    let size = arr.length;    document.write(xorSum(arr, size)); // This code is contributed by Surbhi Tyagi. </script>
28
Time complexity: O(n) Auxiliary space: O(1)
Related Problems: Given a set, find XOR of the XOR’s of all subsets. Find sum of sum of all sub-sequences
This article is contributed by Shubham Bansal. 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. 
nitin mittal
DivyanshuShekhar1
surbhityagi15
shaiksarfraj14
dwivediyash
AshokJaiswal
phasing17
hardikkoriintern
Bitwise-XOR
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.