# Sum of XOR of all pairs in an array

Given an array of n integers, find the sum of xor of all pairs of numbers in the array.

Examples :

```Input : arr[] = {7, 3, 5}
Output : 12
7 ^ 3 = 4
3 ^ 5 = 6
7 ^ 5 = 2
Sum = 4 + 6 + 2
= 12

Input : arr[] = {5, 9, 7, 6}
Output : 47
5 ^ 9 = 12
9 ^ 7 = 14
7 ^ 6 = 1
5 ^ 7 = 2
5 ^ 6 = 3
9 ^ 6 = 15
Sum = 12 + 14 + 1 + 2 + 3 + 15
= 47
```

Recommended Practice

Naive Solution
A Brute Force approach is to run two loops and time complexity is O(n2).

## C++

 `// A Simple C++ program to compute``// sum of bitwise OR of all pairs``#include ``using` `namespace` `std;` `// Returns sum of bitwise OR``// of all pairs``int` `pairORSum(``int` `arr[], ``int` `n)``{``    ``int` `ans = 0; ``// Initialize result` `    ``// Consider all pairs (arr[i], arr[j) such that``    ``// i < j``    ``for` `(``int` `i = 0; i < n; i++)``        ``for` `(``int` `j = i + 1; j < n; j++)``            ``ans += arr[i] ^ arr[j];` `    ``return` `ans;``}` `// Driver program to test above function``int` `main()``{``    ``int` `arr[] = { 5, 9, 7, 6 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << pairORSum(arr, n) << endl;``    ``return` `0;``}`

## Java

 `// A Simple Java program to compute``// sum of bitwise OR of all pairs``import` `java.io.*;` `class` `GFG {``    ` `            ` `    ``// Returns sum of bitwise OR``    ``// of all pairs``    ``static` `int` `pairORSum(``int` `arr[], ``int` `n)``    ``{``        ``// Initialize result``        ``int` `ans = ``0``;``    ` `        ``// Consider all pairs (arr[i], arr[j)``        ``// such that i < j``        ``for` `(``int` `i = ``0``; i < n; i++)``            ``for` `(``int` `j = i + ``1``; j < n; j++)``                ``ans += arr[i] ^ arr[j];``    ` `        ``return` `ans;``    ``}` `    ``// Driver program to test above function``    ``public` `static` `void` `main (String[] args) {``   ` `        ``int` `arr[] = { ``5``, ``9``, ``7``, ``6` `};``        ``int` `n = arr.length;``        ` `        ``System.out.println(pairORSum(arr,``                                ``arr.length));``    ``}``}`  `// This code is contributed by vt_m`

## Python3

 `# A Simple Python 3 program to compute``# sum of bitwise OR of all pairs` `# Returns sum of bitwise OR``# of all pairs``def` `pairORSum(arr, n) :``    ``ans ``=` `0`     `# Initialize result` `    ``# Consider all pairs (arr[i], arr[j)``    ``# such that i < j``    ``for` `i ``in` `range``(``0``, n) :``        ` `        ``for` `j ``in` `range``(i ``+` `1``, n) :``            ` `            ``ans ``=` `ans ``+` `(arr[i] ^ arr[j])``            ` `    ``return` `ans``    `  `# Driver Code``arr ``=` `[ ``5``, ``9``, ``7``, ``6` `]``n ``=` `len``(arr)` `print``(pairORSum(arr, n))`   `# This code is contributed by Nikita Tiwari.`

## C#

 `// A Simple C# program to compute``// sum of bitwise OR of all pairs``using` `System;` `class` `GFG {``    ` `            ` `    ``// Returns sum of bitwise OR``    ``// of all pairs``    ``static` `int` `pairORSum(``int` `[]arr, ``int` `n)``    ``{``        ``// Initialize result``        ``int` `ans = 0;``    ` `        ``// Consider all pairs (arr[i], arr[j)``        ``// such that i < j``        ``for` `(``int` `i = 0; i < n; i++)``            ``for` `(``int` `j = i + 1; j < n; j++)``                ``ans += arr[i] ^ arr[j];``    ` `        ``return` `ans;``    ``}` `    ``// Driver program to test above function``    ``public` `static` `void` `Main () {``    ` `        ``int` `[]arr = { 5, 9, 7, 6 };``        ``int` `n = arr.Length;``        ` `        ``Console.WriteLine(pairORSum(arr,``                                ``arr.Length));``    ``}``}`  `// This code is contributed by vt_m`

## PHP

 `

## JavaScript

 ```?>`

Output :

`47`

Efficient Solution
An Efficient Solution can solve this problem in O(n) time. The assumption here is that integers are represented using 32 bits.
Optimized solution will be to try bit manipulation. To implement the solution, we consider all bits which are 1 and which are 0 and store their count in two different variables. Next multiple those counts along with the power of 2 raised to that bit position. Do this for all the bit positions of the numbers. Their sum would be our answer.
How this actually works?

For example, look at the rightmost bit of all the numbers in the array. Suppose that a numbers have a rightmost 0-bit, and b numbers have a 1-bit. Then out of the pairs, a*b of them will have 1 in the rightmost bit of the XOR. This is because there are a*b ways to choose one number that has a 0-bit and one that has a 1-bit. These bits will therefore contribute a*b towards the total of all the XORs.

In general, when looking at the nth bit (where the rightmost bit is the 0th), count how many numbers have 0 (call this an) and how many have 1 (call this bn). The contribution towards the final sum will be an*bn*pow(2,n). You need to do this for each bit and sum all these contributions together.

This can be done in O(kn) time, where k is the number of bits in the given values.

```Explanation :  arr[] = { 7, 3, 5 }
7 = 1 1 1
3 = 0 1 1
5 = 1 0 1
For bit position 0 :
Bits with zero = 0
Bits with one = 3
Answer = 0 * 3 * 2 ^ 0 = 0
Similarly, for bit position 1 :
Bits with zero = 1
Bits with one = 2
Answer = 1 * 2 * 2 ^ 1 = 4
Similarly, for bit position 2 :
Bits with zero = 1
Bits with one = 2
Answer = 1 * 2 * 2 ^ 2 = 8
Final answer = 0 + 4 + 8 = 12

```

## CPP

 `// An efficient C++ program to compute``// sum of bitwise OR of all pairs``#include ``using` `namespace` `std;` `// Returns sum of bitwise OR``// of all pairs``long` `long` `int` `sumXOR(``int` `arr[], ``int` `n)``{``    ``long` `long` `int` `sum = 0;``    ``for` `(``int` `i = 0; i < 32; i++)``    ``{``        ``//  Count of zeros and ones``        ``int` `zc = 0, oc = 0;``        ` `        ``// Individual sum at each bit position``        ``long` `long` `int` `idsum = 0;``        ``for` `(``int` `j = 0; j < n; j++)``        ``{``            ``if` `(arr[j] % 2 == 0)``                ``zc++;``            ``else``                ``oc++;``            ``arr[j] /= 2;``        ``}``        ` `        ``// calculating individual bit sum``        ``idsum = oc * zc * (1 << i);` `        ``// final sum   ``        ``sum += idsum;``    ``}``    ``return` `sum;``}` `int` `main()``{``    ``long` `long` `int` `sum = 0;``    ``int` `arr[] = { 5, 9, 7, 6 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``sum = sumXOR(arr, n);``    ``cout << sum;``    ``return` `0;``}`

## Java

 `// An efficient Java program to compute``// sum of bitwise OR of all pairs``import` `java.io.*;` `class` `GFG {``    ` `    ``// Returns sum of bitwise OR``    ``// of all pairs``    ``static` `long` `sumXOR(``int` `arr[], ``int` `n)``    ``{``        ``long` `sum = ``0``;``        ``for` `(``int` `i = ``0``; i < ``32``; i++)``        ``{``            ``// Count of zeros and ones``            ``int` `zc = ``0``, oc = ``0``;``            ` `            ``// Individual sum at each bit position``            ``long` `idsum = ``0``;``            ` `            ``for` `(``int` `j = ``0``; j < n; j++)``            ``{``                ``if` `(arr[j] % ``2` `== ``0``)``                    ``zc++;``                    ` `                ``else``                    ``oc++;``                ``arr[j] /= ``2``;``            ``}``            ` `            ``// calculating individual bit sum``            ``idsum = oc * zc * (``1` `<< i);``    ` `            ``// final sum``            ``sum += idsum;``        ``}``        ``return` `sum;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``long` `sum = ``0``;``        ``int` `arr[] = { ``5``, ``9``, ``7``, ``6` `};``        ``int` `n = arr.length;``        ` `        ``sum = sumXOR(arr, n);``        ``System.out.println(sum);``    ``}``}` `// This code is contributed by Nikita Tiwari.`

## Python3

 `# An efficient Python3 program to compute``# sum of bitwise OR of all pair` `# Returns sum of bitwise OR``# of all pairs``def` `sumXOR( arr,  n):``    ` `    ``sum` `=` `0``    ``for` `i ``in` `range``(``0``, ``32``):` `        ``#  Count of zeros and ones``        ``zc ``=` `0``        ``oc ``=` `0``         ` `        ``# Individual sum at each bit position``        ``idsum ``=` `0``        ``for` `j ``in` `range``(``0``, n):``            ``if` `(arr[j] ``%` `2` `=``=` `0``):``                ``zc ``=` `zc ``+` `1``                ` `            ``else``:``                ``oc ``=` `oc ``+` `1``            ``arr[j] ``=` `int``(arr[j] ``/` `2``)``        ` `         ` `        ``# calculating individual bit sum``        ``idsum ``=` `oc ``*` `zc ``*` `(``1` `<< i)`` ` `        ``# final sum   ``        ``sum` `=` `sum` `+` `idsum;``    ` `    ``return` `sum`   `# driver function``sum` `=` `0``arr ``=` `[  ``5``, ``9``, ``7``, ``6` `]``n ``=` `len``(arr)``sum` `=` `sumXOR(arr, n);``print` `(``sum``)` `# This code is contributed by saloni1297`

## C#

 `// An efficient C# program to compute``// sum of bitwise OR of all pairs``using` `System;` `class` `GFG {``    ` `    ``// Returns sum of bitwise OR``    ``// of all pairs``    ``static` `long` `sumXOR(``int` `[]arr, ``int` `n)``    ``{``        ``long` `sum = 0;``        ``for` `(``int` `i = 0; i < 32; i++)``        ``{``            ``// Count of zeros and ones``            ``int` `zc = 0, oc = 0;``            ` `            ``// Individual sum at each bit position``            ``long` `idsum = 0;``            ` `            ``for` `(``int` `j = 0; j < n; j++)``            ``{``                ``if` `(arr[j] % 2 == 0)``                    ``zc++;``                    ` `                ``else``                    ``oc++;``                ``arr[j] /= 2;``            ``}``            ` `            ``// calculating individual bit sum``            ``idsum = oc * zc * (1 << i);``    ` `            ``// final sum``            ``sum += idsum;``        ``}``        ``return` `sum;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``long` `sum = 0;``        ``int` `[]arr = { 5, 9, 7, 6 };``        ``int` `n = arr.Length;``        ` `        ``sum = sumXOR(arr, n);``        ``Console.WriteLine(sum);``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 `

## JavaScript

 `` `?>`

Output:

`47`

My Personal Notes arrow_drop_up