# XOR of Sum of every possible pair of an array

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[2*2] = { 1+1, 1+5, 1+6, 5+1, 5+5, 5+6, 6+1, 6+5, 6+6}
B[4] = { 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```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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

 ` `

Output:

```4
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, ihritik

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.