Skip to content
Related Articles

Related Articles

XOR of Sum of every possible pair of an array
  • Difficulty Level : Easy
  • Last Updated : 10 Feb, 2021
GeeksforGeeks - Summer Carnival Banner

Given an array A of size n. the task is to generate a new sequence B with size N^2 having elements sum of every pair of array A and find the xor value of sum of all the pairs formed. 
Note: Here (A[i], A[i]), (A[i], A[j]), (A[j], A[i]) all are considered as different pairs.
Examples: 

Input: arr = {1, 5, 6}
Output: 4
B[3*3] = { 1+1, 1+5, 1+6, 5+1, 5+5, 5+6, 6+1, 6+5, 6+6}
B[9] = { 2, 6, 7, 6, 10, 11, 7, 11, 12}
So, 2 ^ 6 ^ 7 ^ 6 ^ 10 ^ 11 ^ 7 ^ 6 ^ 11 ^ 12 = 4

Input :1, 2
Output :6

A Naive approach is to run two loops. Consider each and every pair, take their sum and calculate the xor value of the sum of all the pairs.
An Efficient approach is based upon the fact that xor of same values is 0. 
All the pairs like (a[i], a[j]) and (a[j], a[i]) will have same sum. So, their xor values will be 0. Only the pairs like (a[i], a[i]) will give the different result. So, take the xor of all the elements of given array and multiply it by 2.

C++




// C++ program to find XOR of
// sum of every possible pairs
// in an array
#include <bits/stdc++.h>
using namespace std;
 
// Function to find XOR of sum
// of all pairs
int findXor(int arr[], int n)
{
 
    // Calculate xor of all the elements
    int xoR = 0;
    for (int i = 0; i < n; i++) {
        xoR = xoR ^ arr[i];
    }
 
    // Return twice of xor value
    return xoR * 2;
}
 
// Drivers code
int main()
{
    int arr[3] = { 1, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findXor(arr, n);
 
    return 0;
}

Java




// Java program to find XOR of
// sum of every possible pairs
// in an array
import java.io.*;
 
class GFG {
     
    // Function to find XOR of sum
    // of all pairs
    static int findXor(int arr[], int n)
    {
     
        // Calculate xor of all the
        // elements
        int xoR = 0;
        for (int i = 0; i < n; i++) {
            xoR = xoR ^ arr[i];
        }
     
        // Return twice of xor value
        return xoR * 2;
    }
     
    // Drivers code
    public static void main (String[] args)
    {
        int arr[] = { 1, 5, 6 };
        int n = arr.length;
        System.out.println( findXor(arr, n));
    }
}
 
// This code is contributed by anuj_67.

Python3




# Python3 program to find
# XOR of sum of every
# possible pairs in an array
 
# Function to find XOR
# of sum of all pairs
def findXor(arr,n):
 
    # Calculate xor of
    # all the elements
    xoR = 0;
    for i in range (0, n ) :
        xoR = xoR ^ arr[i]
     
    # Return twice of
    # xor value
    return xoR * 2
 
# Driver code
arr = [ 1, 5, 6 ]
n = len(arr)
print(findXor(arr, n))
 
# This code is contributed
# by ihritik
    

C#




// C# program to find XOR of
// sum of every possible pairs
// in an array
using System;
 
class GFG {
     
    // Function to find XOR of sum
    // of all pairs
    static int findXor(int []arr, int n)
    {
     
        // Calculate xor of all the
        // elements
        int xoR = 0;
        for (int i = 0; i < n; i++) {
            xoR = xoR ^ arr[i];
        }
     
        // Return twice of xor value
        return xoR * 2;
    }
     
    // Drivers code
    public static void Main ()
    {
        int []arr = { 1, 5, 6 };
        int n = arr.Length;
        Console.WriteLine( findXor(arr, n));
    }
}
 
// This code is contributed by anuj_67.

PHP




<?php
// PHP program to find XOR
// of sum of every possible
// pairs in an array
 
// Function to find XOR
// of sum of all pairs
function findXor($arr, $n)
{
 
    // Calculate xor of
    // all the elements
    $xoR = 0;
    for ($i = 0; $i < $n; $i++)
    {
        $xoR = $xoR ^ $arr[$i];
    }
 
    // Return twice
    // of xor value
    return $xoR * 2;
}
 
// Driver code
$arr = array(1, 5, 6);
$n = count($arr);
 
echo findXor($arr, $n);
 
// This code is contributed
// by anuj_67.
?>
Output: 
4

 

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 :