Sum of XOR of all possible subsets

Given an array arr[] of size n, we need to find sum of all the values that comes 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 combination 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 large value of 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 number of the XORs, we can observe that set bit occurs at all the position of i(0 to n-1) will exactly contribute to half of 2n. So we can easily imposed these two condition at each such positions of i.

  • If there is any value of arr[] that has set tth 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:
If 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is C++ approach to finding the XOR_SUM
#include<bits/stdc++.h>
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);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right


Output:

 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

Reference:
http://math.stackexchange.com/questions/712487/finding-xor-of-all-subsets?newreg=293ddec5b7614b7fa4c50b4e4d710a4b

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.