Bitwise XOR of all unordered pairs from a given array
Given an array arr[] of size N, the task is to find the bitwise XOR of all possible unordered pairs of the given array.
Examples:
Input: arr[] = {1, 5, 3, 7}
Output: 0
Explanation:
All possible unordered pairs are (1, 5), (1, 3), (1, 7), (5, 3), (5, 7), (3, 7)
Bitwise XOR of all possible pairs are = 1 ^ 5 ^ 1 ^3 ^ 1 ^ 7 ^ 5 ^ 3 ^ 5^ 7 ^ 3 ^ 7 = 0
Therefore, the required output is 0.
Input: arr[] = {1, 2, 3, 4}
Output: 4
Naive approach: The idea is to traverse the array and generate all possible pairs of the given array. Finally, print the Bitwise XOR of each element present in these pairs of the given array. Follow the steps below to solve the problem:
- Initialize a variable, say totalXOR, to store Bitwise XOR of each element from these pairs.
- Traverse the given array and generate all possible pairs(arr[i], arr[j]) from the given array.
- For each pair (arr[i], arr[j]), update the value of totalXOR = (totalXOR ^ arr[i] ^ arr[j]).
- Finally, print the value of totalXOR.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int TotalXorPair( int arr[], int N)
{
int totalXOR = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N;
j++) {
totalXOR ^= arr[i]
^ arr[j];
}
}
return totalXOR;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << TotalXorPair(arr, N);
}
|
Java
class GFG{
public static int TotalXorPair( int arr[],
int N)
{
int totalXOR = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
totalXOR ^= arr[i] ^ arr[j];
}
}
return totalXOR;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
System.out.print(TotalXorPair(arr, N));
}
}
|
Python3
def TotalXorPair(arr, N):
totalXOR = 0 ;
for i in range ( 0 , N):
for j in range (i + 1 , N):
totalXOR ^ = arr[i] ^ arr[j];
return totalXOR;
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 ];
N = len (arr);
print (TotalXorPair(arr, N));
|
C#
using System;
class GFG{
public static int TotalXorPair( int []arr,
int N)
{
int totalXOR = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
totalXOR ^= arr[i] ^ arr[j];
}
}
return totalXOR;
}
public static void Main(String[] args)
{
int []arr = {1, 2, 3, 4};
int N = arr.Length;
Console.Write(TotalXorPair(arr, N));
}
}
|
Javascript
<script>
function TotalXorPair(arr, N)
{
let totalXOR = 0;
for (let i = 0; i < N; i++)
{
for (let j = i + 1; j < N; j++)
{
totalXOR ^= arr[i] ^ arr[j];
}
}
return totalXOR;
}
let arr = [ 1, 2, 3, 4 ];
let N = arr.length;
document.write(TotalXorPair(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, follow the observations below:
Property of Bitwise XOR:
a ^ a ^ a …….( Even times ) = 0
a ^ a ^ a …….( Odd times ) = a
Each element of the given array occurs exactly (N – 1) times in all possible pairs.
Therefore, if N is even, then Bitwise XOR of all possible pairs are equal to bitwise XOR of all the array elements.
Otherwise, bitwise XOR of all possible pairs are equal to 0.
Follow the steps below to solve the problem:
- If N is odd then print 0.
- If N is even then print the value of bitwise XOR of all the elements of the given array.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int TotalXorPair( int arr[], int N)
{
int totalXOR = 0;
if (N % 2 != 0) {
return 0;
}
for ( int i = 0; i < N; i++) {
totalXOR ^= arr[i];
}
return totalXOR;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << TotalXorPair(arr, N);
}
|
Java
class GFG{
public static int TotalXorPair( int arr[],
int N)
{
int totalXOR = 0 ;
if ( N % 2 != 0 )
{
return 0 ;
}
for ( int i = 0 ; i < N; i++)
{
totalXOR ^= arr[i];
}
return totalXOR;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
System.out.print(TotalXorPair(arr, N));
}
}
|
Python3
def TotalXorPair(arr, N):
totalXOR = 0
if (N % 2 ! = 0 ):
return 0
for i in range (N):
totalXOR ^ = arr[i]
return totalXOR
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
print (TotalXorPair(arr, N))
|
C#
using System;
class GFG{
static int TotalXorPair( int []arr,
int N)
{
int totalXOR = 0;
if (N % 2 != 0)
{
return 0;
}
for ( int i = 0; i < N; i++)
{
totalXOR ^= arr[i];
}
return totalXOR;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4 };
int N = arr.Length;
Console.Write(TotalXorPair(arr, N));
}
}
|
Javascript
<script>
function TotalXorPair(arr, N)
{
let totalXOR = 0;
if (N % 2 != 0) {
return 0;
}
for (let i = 0; i < N; i++) {
totalXOR ^= arr[i];
}
return totalXOR;
}
let arr = [ 1, 2, 3, 4 ];
let N = arr.length;
document.write(TotalXorPair(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
26 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...