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
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. ?> |
4
Recommended Posts:
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Maximum OR value of a pair in an array
- Pair with largest sum which is less than K in the array
- Smallest Pair Sum in an array
- Quadruplet pair with XOR zero in the given Array
- Maximum AND value of a pair in an array
- Print pair with maximum AND value in an array
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Construct an array from its pair-product
- Closest product pair in an array
- Check if product of every pair exists in an array
- Find pair with greatest product in array
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Minimum array size after repeated replacement of even sum pair with sum
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.


