Related Articles

Related Articles

Sum of XOR of sum of all pairs in an array
  • Difficulty Level : Medium
  • Last Updated : 19 Sep, 2018

Given an array, find the XOR of sum of all pairs in an array.

Examples:

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

A naive approach is to consider all the pairs one by one, calculate their XOR one after the other.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find XOR of pair
// sums.
#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;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << xorPairSum(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find XOR of pair sums.
import java.io.*;
   
class GFG {
  
// method to find XOR of pair sums
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;
}
   
    // Driver code
    public static void main (String[] args) 
    {
        int arr[] = {1, 2, 3};
        int n = arr.length;
        System.out.print( xorPairSum(arr, n));
    }
}
  
// This code is contributed by chandan_jnu.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find 
# XOR of pair sums.
  
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
  
  
# Driver program to test the above function
if __name__ == "__main__":
    data = [1, 2, 3]
    print(xor_pair_sum(data, len(data)))
  
# This code is contributed
# by Kanav Malhotra

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find
// XOR of pair sums.
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;
}
  
// Driver code
static public void Main(String []args)
{
    int []arr = { 1, 2, 3 };
    int n = arr.Length;
    Console.WriteLine(xorPairSum(arr, n));
}
}
  
// This code is contributed
// by Arnab Kundu

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// XOR of pair sums.
  
function xorPairSum($ar, $n)
{
    $sum = 0;
    for ($i = 0; $i < $n; $i++) 
        $sum = $sum ^ ($ar[$i] + 
                       $ar[$j]);
    return $sum;
}
  
// Driver code
$arr = array( 1, 2, 3 );
$n = count($arr);
echo xorPairSum($arr, $n);
  
// This code is contributed 
// by Subhadeep
?>

chevron_right


Output:

0

Time Complexity : O(N2)



An efficient solution is based on XOR properties. We simply calculate the XOR of every element and then just multiply it by two.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find XOR of pair
// sums.
#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;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << xorPairSum(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find 
// XOR of pair sums.
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;
}
  
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 2, 3 };
    int n = arr.length;
    System.out.println( xorPairSum(arr, n));
}
}
  
// This code is contributed 
// by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# XOR of pair sums.
  
def xor_pair_sum(ar, n):
    total = 0
    for i in range(n):
        total = total ^ ar[i]
  
    return 2 * total
  
  
# Driver program to test the above function
if __name__ == "__main__":
    data = [1, 2, 3]
    print(xor_pair_sum(data, len(data)))
  
# This code is contributed
# by Kanav Malhotra

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find 
// XOR of pair sums.
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;
}
  
// Driver code
static public void Main(String []args)
{
    int []arr = { 1, 2, 3 };
    int n = arr.Length;
    Console.WriteLine( xorPairSum(arr, n));
}
}
  
// This code is contributed
// by Arnab Kundu

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// XOR of pair sums.
  
function xor_pair_sum($ar, $n)
{
    $total = 0;
    for($i = 0; $i < $n; $i++)
        $total = $total ^ $ar[$i];
  
    return (2 * $total);
}
  
// Driver Code
$data = array(1, 2, 3);
$n = sizeof($data);
echo xor_pair_sum($data, $n);
  
// This code is contributed
// by mits
?>

chevron_right


Output:

0

Time Complexity : O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :