Given an array, find the XOR of sum of all pairs in an array.
Input : arr[] = {1, 2, 3}
Output : 0
(1 + 1) ^ (1 + 2) ^ (1 + 3) ^ (2 + 1) ^ (2 + 2) ^
(2 + 3) ^ (3 + 1) ^ (3 + 2) ^ (3 + 3) = 0
Input : arr[] = {1, 2, 3, 4}
Output : 8
Implementation: A naive approach is to consider all the pairs one by one, calculate their XOR one after the other.
C++
#include <bits/stdc++.h>
using namespace std;
int xorPairSum( int ar[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
sum = sum ^ (ar[i] + ar[j]);
return sum;
}
int main()
{
int arr[] = { 1, 2, 3 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << xorPairSum(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int xorPairSum( int ar[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n; j++)
sum = sum ^ (ar[i] + ar[j]);
return sum;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 };
int n = arr.length;
System.out.print( xorPairSum(arr, n));
}
}
|
Python3
def xor_pair_sum(ar, n):
total = 0
for i in range (n):
for j in range (n):
total = total ^ (ar[i] + ar[j])
return total
if __name__ = = "__main__" :
data = [ 1 , 2 , 3 ]
print (xor_pair_sum(data, len (data)))
|
C#
using System;
class GFG
{
static int xorPairSum( int []ar,
int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
sum = sum ^ (ar[i] + ar[j]);
return sum;
}
static public void Main(String []args)
{
int []arr = { 1, 2, 3 };
int n = arr.Length;
Console.WriteLine(xorPairSum(arr, n));
}
}
|
PHP
<?php
function xorPairSum( $ar , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum = $sum ^ ( $ar [ $i ] +
$ar [ $j ]);
return $sum ;
}
$arr = array ( 1, 2, 3 );
$n = count ( $arr );
echo xorPairSum( $arr , $n );
?>
|
Javascript
<script>
function xorPairSum(ar, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
for (let j = 0; j < n; j++)
sum = sum ^ (ar[i] + ar[j]);
return sum;
}
let arr = [ 1, 2, 3 ];
let n = arr.length;
document.write(xorPairSum(arr, n));
</script>
|
Implementation: An efficient solution is based on XOR properties. We simply calculate the XOR of every element and then just multiply it by two.
C++
#include <bits/stdc++.h>
using namespace std;
int xorPairSum( int ar[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum = sum ^ ar[i];
return 2*sum;
}
int main()
{
int arr[] = { 1, 2, 3 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << xorPairSum(arr, n);
return 0;
}
|
Java
class GFG
{
static int xorPairSum( int ar[],
int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum = sum ^ ar[i];
return 2 * sum;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 };
int n = arr.length;
System.out.println( xorPairSum(arr, n));
}
}
|
Python3
def xor_pair_sum(ar, n):
total = 0
for i in range (n):
total = total ^ ar[i]
return 2 * total
if __name__ = = "__main__" :
data = [ 1 , 2 , 3 ]
print (xor_pair_sum(data, len (data)))
|
C#
using System;
class GFG
{
static int xorPairSum( int []ar,
int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum = sum ^ ar[i];
return 2 * sum;
}
static public void Main(String []args)
{
int []arr = { 1, 2, 3 };
int n = arr.Length;
Console.WriteLine( xorPairSum(arr, n));
}
}
|
PHP
<?php
function xor_pair_sum( $ar , $n )
{
$total = 0;
for ( $i = 0; $i < $n ; $i ++)
$total = $total ^ $ar [ $i ];
return (2 * $total );
}
$data = array (1, 2, 3);
$n = sizeof( $data );
echo xor_pair_sum( $data , $n );
?>
|
Javascript
<script>
function xorPairSum(ar, n)
{
var sum = 0;
for (i = 0; i < n; i++)
sum = sum ^ ar[i];
return 2 * sum;
}
var arr = [ 1, 2, 3 ];
var n = arr.length;
document.write( xorPairSum(arr, n));
</script>
|
Contact Us