XOR of all possible pairwise sum from two given Arrays
Last Updated :
14 Jun, 2021
Given two arrays A[] and B[] of equal length, the task is to find the Bitwise XOR of the pairwise sum of the given two arrays.
Examples:
Input: A[] = {1, 2}, B[] = {3, 4}
Output: 2
Explanation:
Sum of all possible pairs are {4(1 + 3), 5(1 + 4), 5(2 + 3), 6(2 + 4)}
XOR of all the pair sums = 4 ^ 5 ^ 5 ^ 6 = 2
Input: A[] = {4, 6, 0, 0, 3, 3}, B[] = {0, 5, 6, 5, 0, 3}
Output: 8
Naive Approach: The simplest approach to solve the problem is to generate all possible pairs from the two given arrays and calculate their respective sums and update XOR with the sum of pairs. Finally, print the XOR obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int XorSum( int A[], int B[], int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
ans = ans ^ (A[i] + B[j]);
}
}
return ans;
}
int main()
{
int A[] = { 4, 6, 0, 0, 3, 3 };
int B[] = { 0, 5, 6, 5, 0, 3 };
int N = sizeof A / sizeof A[0];
cout << XorSum(A, B, N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
static int XorSum( int A[], int B[], int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
ans = ans ^ (A[i] + B[j]);
}
}
return ans;
}
public static void main (String[] args)
{
int A[] = { 4 , 6 , 0 , 0 , 3 , 3 };
int B[] = { 0 , 5 , 6 , 5 , 0 , 3 };
int N = A.length;
System.out.println(XorSum(A, B, N));
}
}
|
Python3
def XorSum(A, B, N):
ans = 0
for i in range (N):
for j in range (N):
ans = ans ^ (A[i] + B[j])
return ans
if __name__ = = "__main__" :
A = [ 4 , 6 , 0 , 0 , 3 , 3 ]
B = [ 0 , 5 , 6 , 5 , 0 , 3 ]
N = len (A)
print (XorSum(A, B, N))
|
C#
using System;
class GFG{
static int XorSum( int []A, int []B, int N)
{
int ans = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
ans = ans ^ (A[i] + B[j]);
}
}
return ans;
}
public static void Main(String[] args)
{
int []A = {4, 6, 0, 0, 3, 3};
int []B = {0, 5, 6, 5, 0, 3};
int N = A.Length;
Console.WriteLine(XorSum(A, B, N));
}
}
|
Javascript
<script>
function XorSum(A , B , N) {
var ans = 0;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
ans = ans ^ (A[i] + B[j]);
}
}
return ans;
}
var A = [ 4, 6, 0, 0, 3, 3 ];
var B = [ 0, 5, 6, 5, 0, 3 ];
var N = A.length;
document.write(XorSum(A, B, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using the Bit Manipulation technique. Follow the steps below to solve the problem:
- Considering only the Kth bit, the task is to count the number of pairs (i, j) such that the Kth bit of (Ai + Bj) is set.
- If this number is odd, add X = 2k to the answer. We are only interested in the values of (ai, bj) in modulo 2X.
- Thus, replace ai with ai % (2X) and bj with bj % (2X), and assume that ai and bj < 2X.
- There are two cases when the kth bit of (ai + bj) is set:
- x ? ai + bj < 2x
- 3x ? ai + bj < 4x
- Hence, sort b[] in increasing order. For a fixed i, the set of j that satisfies X ? (ai +bj) < 2X forms an interval.
- Therefore, count the number of such j by Binary search. Similarly, handle the second case.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int XorSum( int A[], int B[], int N)
{
const int maxBit = 29;
int ans = 0;
for ( int k = 0; k < maxBit; k++) {
int C[N];
for ( int i = 0; i < N; i++) {
C[i] = B[i] % (1 << (k + 1));
}
sort(C, C + N);
long long count = 0;
long long l, r;
for ( int i = 0; i < N; i++) {
int x = A[i] % (1 << (k + 1));
l = lower_bound(C,
C + N,
(1 << k) - x)
- C;
r = lower_bound(C,
C + N,
(1 << k) * 2 - x)
- C;
count += (r - l);
l = lower_bound(C,
C + N,
(1 << k) * 3 - x)
- C;
r = lower_bound(C,
C + N,
(1 << k) * 4 - x)
- C;
count += (r - l);
}
if (count & 1)
ans += (1 << k);
}
return ans;
}
int main()
{
int A[] = { 4, 6, 0, 0, 3, 3 };
int B[] = { 0, 5, 6, 5, 0, 3 };
int N = sizeof A / sizeof A[0];
cout << XorSum(A, B, N) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int lower_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2 ;
if (element > a[middle])
low = middle + 1 ;
else
high = middle;
}
return low;
}
static int XorSum( int A[],
int B[], int N)
{
final int maxBit = 29 ;
int ans = 0 ;
for ( int k = 0 ; k < maxBit; k++)
{
int []C = new int [N];
for ( int i = 0 ; i < N; i++)
{
C[i] = B[i] % ( 1 << (k + 1 ));
}
Arrays.sort(C);
long count = 0 ;
long l, r;
for ( int i = 0 ; i < N; i++)
{
int x = A[i] % ( 1 << (k + 1 ));
l = lower_bound(C, 0 , N,
( 1 << k) - x);
r = lower_bound(C, 0 , N,
( 1 << k) *
2 - x);
count += (r - l);
l = lower_bound(C, 0 , N,
( 1 << k) *
3 - x);
r = lower_bound(C, 0 , N,
( 1 << k) *
4 - x);
count += (r - l);
}
if ((count & 1 ) != 0 )
ans += ( 1 << k);
}
return ans;
}
public static void main(String[] args)
{
int A[] = { 4 , 6 , 0 , 0 , 3 , 3 };
int B[] = { 0 , 5 , 6 , 5 , 0 , 3 };
int N = A.length;
System.out.print(XorSum(A, B,
N) + "\n" );
}
}
|
Python3
from bisect import bisect, bisect_left, bisect_right
def XorSum(A, B, N):
maxBit = 29
ans = 0
for k in range (maxBit):
C = [ 0 ] * N
for i in range (N):
C[i] = B[i] % ( 1 << (k + 1 ))
C = sorted (C)
count = 0
l, r = 0 , 0
for i in range (N):
x = A[i] % ( 1 << (k + 1 ))
l = bisect_left(C, ( 1 << k) - x)
r = bisect_left(C, ( 1 << k) * 2 - x)
count + = (r - l)
l = bisect_left(C, ( 1 << k) * 3 - x)
r = bisect_left(C, ( 1 << k) * 4 - x)
count + = (r - l)
if (count & 1 ):
ans + = ( 1 << k)
return ans
if __name__ = = '__main__' :
A = [ 4 , 6 , 0 , 0 , 3 , 3 ]
B = [ 0 , 5 , 6 , 5 , 0 , 3 ]
N = len (A)
print (XorSum(A, B, N))
|
C#
using System;
class GFG{
static int lower_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2;
if (element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
static int XorSum( int [] A, int [] B, int N)
{
int maxBit = 29;
int ans = 0;
for ( int k = 0; k < maxBit; k++)
{
int [] C = new int [N];
for ( int i = 0; i < N; i++)
{
C[i] = B[i] % (1 << (k + 1));
}
Array.Sort(C);
long count = 0;
long l, r;
for ( int i = 0; i < N; i++)
{
int x = A[i] % (1 << (k + 1));
l = lower_bound(C, 0, N,
(1 << k) - x);
r = lower_bound(C, 0, N,
(1 << k) * 2 - x);
count += (r - l);
l = lower_bound(C, 0, N,
(1 << k) * 3 - x);
r = lower_bound(C, 0, N,
(1 << k) * 4 - x);
count += (r - l);
}
if ((count & 1) != 0)
ans += (1 << k);
}
return ans;
}
public static void Main( string [] args)
{
int [] A = { 4, 6, 0, 0, 3, 3 };
int [] B = { 0, 5, 6, 5, 0, 3 };
int N = A.Length;
Console.Write(XorSum(A, B, N) + "\n" );
}
}
|
Javascript
<script>
function lower_bound(a,low,high,element)
{
while (low < high)
{
let middle = low + Math.floor((high - low) / 2);
if (element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
function XorSum(A,B,N)
{
let maxBit = 29;
let ans = 0;
for (let k = 0; k < maxBit; k++)
{
let C = new Array(N);
for (let i = 0; i < N; i++)
{
C[i] = B[i] % (1 << (k + 1));
}
C.sort( function (x,y){ return x-y;});
let count = 0;
let l, r;
for (let i = 0; i < N; i++)
{
let x = A[i] % (1 << (k + 1));
l = lower_bound(C, 0, N,
(1 << k) - x);
r = lower_bound(C, 0, N,
(1 << k) *
2 - x);
count += (r - l);
l = lower_bound(C, 0, N,
(1 << k) *
3 - x);
r = lower_bound(C, 0, N,
(1 << k) *
4 - x);
count += (r - l);
}
if ((count & 1) != 0)
ans += (1 << k);
}
return ans;
}
let A=[4, 6, 0, 0, 3, 3];
let B=[0, 5, 6, 5, 0, 3];
let N = A.length;
document.write(XorSum(A, B,
N) + "\n" );
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...