Sum of XOR of all pairs in an array
Last Updated :
29 Mar, 2024
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
Naive Solution: A Brute Force approach is to run two loops and time complexity is O(n2).
C++
#include <bits/stdc++.h>
using namespace std;
int pairORSum( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
ans += arr[i] ^ arr[j];
return ans;
}
int main()
{
int arr[] = { 5, 9, 7, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << pairORSum(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int pairORSum( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = i + 1 ; j < n; j++)
ans += arr[i] ^ arr[j];
return ans;
}
public static void main (String[] args) {
int arr[] = { 5 , 9 , 7 , 6 };
int n = arr.length;
System.out.println(pairORSum(arr,
arr.length));
}
}
|
Python3
def pairORSum(arr, n) :
ans = 0
for i in range ( 0 , n) :
for j in range (i + 1 , n) :
ans = ans + (arr[i] ^ arr[j])
return ans
arr = [ 5 , 9 , 7 , 6 ]
n = len (arr)
print (pairORSum(arr, n))
|
C#
using System;
class GFG {
static int pairORSum( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
ans += arr[i] ^ arr[j];
return ans;
}
public static void Main () {
int []arr = { 5, 9, 7, 6 };
int n = arr.Length;
Console.WriteLine(pairORSum(arr,
arr.Length));
}
}
|
Javascript
const pairORSum = (arr, n) => {
let ans = 0;
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++)
ans += arr[i] ^ arr[j];
return ans;
}
let arr = [5, 9, 7, 6];
let n = arr.length;
document.write(pairORSum(arr, n));
|
PHP
<?php
function pairORSum( $arr , $n )
{
$ans = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $i + 1; $j < $n ; $j ++)
$ans += $arr [ $i ] ^ $arr [ $j ];
return $ans ;
}
$arr = array ( 5, 9, 7, 6 );
$n = count ( $arr );
echo pairORSum( $arr , $n ) ;
?>
|
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 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
#include <bits/stdc++.h>
using namespace std;
long long int sumXOR( int arr[], int n)
{
long long int sum = 0;
for ( int i = 0; i < 32; i++)
{
int zc = 0, oc = 0;
long long int idsum = 0;
for ( int j = 0; j < n; j++)
{
if (arr[j] % 2 == 0)
zc++;
else
oc++;
arr[j] /= 2;
}
idsum = oc * zc * (1 << i);
sum += idsum;
}
return sum;
}
int main()
{
long long int sum = 0;
int arr[] = { 5, 9, 7, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
sum = sumXOR(arr, n);
cout << sum;
return 0;
}
|
Java
import java.io.*;
class GFG {
static long sumXOR( int arr[], int n)
{
long sum = 0 ;
for ( int i = 0 ; i < 32 ; i++)
{
int zc = 0 , oc = 0 ;
long idsum = 0 ;
for ( int j = 0 ; j < n; j++)
{
if (arr[j] % 2 == 0 )
zc++;
else
oc++;
arr[j] /= 2 ;
}
idsum = oc * zc * ( 1 << i);
sum += idsum;
}
return sum;
}
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);
}
}
|
Python3
def sumXOR( arr, n):
sum = 0
for i in range ( 0 , 32 ):
zc = 0
oc = 0
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 )
idsum = oc * zc * ( 1 << i)
sum = sum + idsum;
return sum
sum = 0
arr = [ 5 , 9 , 7 , 6 ]
n = len (arr)
sum = sumXOR(arr, n);
print ( sum )
|
C#
using System;
class GFG {
static long sumXOR( int []arr, int n)
{
long sum = 0;
for ( int i = 0; i < 32; i++)
{
int zc = 0, oc = 0;
long idsum = 0;
for ( int j = 0; j < n; j++)
{
if (arr[j] % 2 == 0)
zc++;
else
oc++;
arr[j] /= 2;
}
idsum = oc * zc * (1 << i);
sum += idsum;
}
return sum;
}
public static void Main()
{
long sum = 0;
int []arr = { 5, 9, 7, 6 };
int n = arr.Length;
sum = sumXOR(arr, n);
Console.WriteLine(sum);
}
}
|
JavaScript
<script>
const sumXOR = (arr, n) => {
let sum = 0;
for (let i = 0; i < 32; i++) {
let zc = 0, oc = 0;
let idsum = 0;
for (let j = 0; j < n; j++) {
if (arr[j] % 2 == 0)
zc++;
else
oc++;
arr[j] = parseInt(arr[j] / 2);
}
idsum = oc * zc * (1 << i);
sum += idsum;
}
return sum;
}
let sum = 0;
let arr = [5, 9, 7, 6];
let n = arr.length;
sum = sumXOR(arr, n);
document.write(sum);
</script>
?>
|
PHP
<?php
function sumXOR( $arr , $n )
{
$sum = 0;
for ( $i = 0; $i < 32; $i ++)
{
$zc = 0; $oc = 0;
$idsum = 0;
for ( $j = 0; $j < $n ; $j ++)
{
if ( $arr [ $j ] % 2 == 0)
$zc ++;
else
$oc ++;
$arr [ $j ] /= 2;
}
$idsum = $oc * $zc * (1 << $i );
$sum += $idsum ;
}
return $sum ;
}
$sum = 0;
$arr = array ( 5, 9, 7, 6 );
$n = count ( $arr );
$sum = sumXOR( $arr , $n );
echo $sum ;
|
Output:
47
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...