XOR of pairwise sum of every unordered pairs in an array - GeeksforGeeks

24 Nov, 2021
Given an array arr[] of length N, the task is to find the XOR of pairwise sum of every possible unordered pairs of the array. The unordered pairs sum is defined as follows –  
XOR of pairwise sum  = (A[0] + A[1]) ^
    (A[0] + A[2]) ^ ...(A[0] + A[N]) ^
    (A[1] + A[2]) ^ ...(A[1] + A[N]) ^
    .......
    (A[N-1] + A[N])

Notice that after including A[0] and A[1] 
as pairs, then A[1] and A[0] are not included.
Examples:  
Input: arr[] = {1, 2} Output: 3 Explanation: There is only one unordered pair. That is (1, 2)Input: arr[] = {1, 2, 3} Output: 2 Explanation: Unordered pairs of the numbers – {(1, 2), (1, 3), (2, 3)} XOR of unordered pairswise sum – => (1 + 2) ^ (1 + 3) ^ (2 + 3) => 3 ^ 4 ^ 5 => 2  
 
Naive Approach: The idea is to find every possible unordered pair with the help of the two loops and find the XOR of these pairs.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation to find XOR of// pairwise sum of every unordered// pairs in an array #include <bits/stdc++.h> using namespace std; // Function to find XOR of pairwise// sum of every unordered pairsint xorOfSum(int a[], int n){    int answer = 0;         // Loop to choose every possible    // pairs in the array    for (int i = 0; i < n; i++) {        for (int j = i + 1; j < n; j++)            answer ^= (a[i] + a[j]);    }     return answer;} // Driver Codeint main(){    int n = 3;    int A[n] = { 1, 2, 3 };     cout << xorOfSum(A, n);    return 0;}
// Java implementation to find XOR of// pairwise sum of every unordered// pairs in an arrayclass GFG{  // Function to find XOR of pairwise// sum of every unordered pairsstatic int xorOfSum(int a[], int n){    int answer = 0;          // Loop to choose every possible    // pairs in the array    for (int i = 0; i < n; i++) {        for (int j = i + 1; j < n; j++)            answer ^= (a[i] + a[j]);    }      return answer;}  // Driver Codepublic static void main(String[] args){    int n = 3;    int A[] = { 1, 2, 3 };      System.out.print(xorOfSum(A, n));}} // This code is contributed by PrinciRaj1992
# Python3 implementation to find XOR of# pairwise sum of every unordered# pairs in an array # Function to find XOR of pairwise# sum of every unordered pairsdef xorOfSum(a, n):    answer = 0     # Loop to choose every possible    # pairs in the array    for i in range(n):        for j in range(i + 1, n):            answer ^= (a[i] + a[j])     return answer # Driver Codeif __name__ == '__main__':    n = 3    A=[1, 2, 3]     print(xorOfSum(A, n)) # This code is contributed by mohit kumar 29
// C# implementation to find XOR of// pairwise sum of every unordered// pairs in an arrayusing System;using System.Collections.Generic; class GFG{   // Function to find XOR of pairwise// sum of every unordered pairsstatic int xorOfSum(int []a, int n){    int answer = 0;           // Loop to choose every possible    // pairs in the array    for (int i = 0; i < n; i++) {        for (int j = i + 1; j < n; j++)            answer ^= (a[i] + a[j]);    }       return answer;}   // Driver Codepublic static void Main(String[] args){    int n = 3;    int []A = { 1, 2, 3 };       Console.Write(xorOfSum(A, n));}}  // This code is contributed by PrinciRaj1992
<script>// JavaScript implementation to find XOR of// pairwise sum of every unordered// pairs in an array // Function to find XOR of pairwise// sum of every unordered pairsfunction xorOfSum(a, n){    let answer = 0;         // Loop to choose every possible    // pairs in the array    for (let i = 0; i < n; i++) {        for (let j = i + 1; j < n; j++)            answer ^= (a[i] + a[j]);    }     return answer;} // Driver Code    let n = 3;    let A = [ 1, 2, 3 ];     document.write(xorOfSum(A, n)); // This code is contributed by Surbhi Tyagi </script>
2
 
Efficient Approach:  
For obtaining Kth bit of the final xor value we see in all the pair-sums, that kth bit of them is set or not. If there are even a number of pairs that have the Kth bit set, then for the Kth bit, their xor is zero else one.  
For finding count of pair sums having Kth bit set, we notice that we can mod all array elements by 2(K+1). This is because X and Y belong to input array and Sum = X + Y. Then X + Y can add up to have their Kth bit set which means Sum >= 2K. It can also be observed that they can have a carryover from addition which makes numbers in the range [2(K+1), 2(K+1) + 2K) have their Kth bit not set. So we only care about the Kth and (K+1)th bit of all the numbers to check the XOR of Kth bit.  
After the mod operation is performed, for sum to have kth bit set, its value will be in range – [2K, 2(K+1) ) U [2(K+1) + 2K, Max-Value-Sum-Can-Take ].  
To find the numbers in the said range, make another array B containing modded array elements of arr[], and sort them. Then Sum can be assumed as Sum = Bi + Bj. Finally, find the maximum bound of j using binary search (built-in lower_bound in C++). Fix i and since the array is sorted find the last j that satisfies the given condition and all the numbers in the range of indices can be added to the count to check the xor.  
Auxiliary Space: O(1)Below is the implementation of the above approach: 
C++
// C++ implementation to find XOR of// pairwise sum of every unordered// pairs in an array #include <bits/stdc++.h> using namespace std; // Function to find XOR of pairwise// sum of every unordered pairsint xorOfSum(int a[], int n){     int i, j, k;         // Sort the array    sort(a, a + n);     int ans = 0;     // Array elements are not greater    // than 1e7 so 27 bits are suffice    for (k = 0; k < 27; ++k) {                 // Modded elements of array        vector<int> b(n);                 // Loop to find the modded        // elements of array        for (i = 0; i < n; i++)            b[i] = a[i] % (1 << (k + 1));         // Sort the modded array        sort(b.begin(), b.end());         int cnt = 0;        for (i = 0; i < n; i++) {            // finding the bound for j            // for given i using binary search            int l = lower_bound(b.begin() +                           i + 1, b.end(),               (1 << k) - b[i]) - b.begin();            int r = lower_bound(b.begin() +                    i + 1, b.end(), (1 << (k + 1)) -                          b[i]) - b.begin();             // All the numbers in the range            // of indices can be added to the            // count to check the xor.            cnt += r - l;             l = lower_bound(b.begin() + i + 1,                 b.end(), (1 << (k + 1)) +                 (1 << k) - b[i]) - b.begin();            cnt += n - l;        }        // Remainder of cnt * kth power        // of 2 added to the xor value        ans += (cnt % 2) * 1LL * (1 << k);    }     return ans;} // Driver Codeint main(){    int n = 3;    int A[n] = { 1, 2, 3 };     cout << xorOfSum(A, n);    return 0;}
2
 
Performance Analysis:  
Time complexity : O(N * log(max(A))*log(N)
Auxiliary Space: O(N)
Outermost loop runs for log(max(A)) times and for each loop we create and sort array b ,which consists of N elements ,hence complexity is O(N*log(N)*log(max(A))) 
mohit kumar 29
princiraj1992
coder72
surbhityagi15
subham348
Bitwise-XOR
Algorithms
Bit Magic
Competitive Programming
Greedy
Mathematical
Searching
Searching
Greedy
Mathematical
Bit Magic
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
How to Start Learning DSA?
Difference between Algorithm, Pseudocode and Program
K means Clustering - Introduction
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?