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++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to get bitwise XOR// of all possible pairs of// the given arrayint TotalXorPair(int arr[], int N){ // Stores bitwise XOR // of all possible pairs int totalXOR = 0; // Generate all possible pairs // and calculate bitwise XOR // of all possible pairs for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { // Calculate bitwise XOR // of each pair totalXOR ^= arr[i] ^ arr[j]; } } return totalXOR;}// Driver Codeint main(){ int arr[] = { 1, 2, 3, 4 }; int N = sizeof(arr) / sizeof(arr[0]); cout << TotalXorPair(arr, N);} |
Java
// Java program to implement // the above approach class GFG{ // Function to get bitwise XOR // of all possible pairs of // the given array public static int TotalXorPair(int arr[], int N) { // Stores bitwise XOR // of all possible pairs int totalXOR = 0; // Generate all possible pairs // and calculate bitwise XOR // of all possible pairs for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { // Calculate bitwise XOR // of each pair totalXOR ^= arr[i] ^ arr[j]; } } return totalXOR; } // Driver code public static void main(String[] args) { int arr[] = {1, 2, 3, 4}; int N = arr.length; System.out.print(TotalXorPair(arr, N));}}// This code is contributed by divyeshrabadiya07 |
C#
// C# program to implement // the above approach using System;class GFG{ // Function to get bitwise XOR // of all possible pairs of // the given array public static int TotalXorPair(int []arr, int N) { // Stores bitwise XOR // of all possible pairs int totalXOR = 0; // Generate all possible pairs // and calculate bitwise XOR // of all possible pairs for(int i = 0; i < N; i++) { for(int j = i + 1; j < N; j++) { // Calculate bitwise XOR // of each pair totalXOR ^= arr[i] ^ arr[j]; } } return totalXOR; } // Driver code public static void Main(String[] args) { int []arr = {1, 2, 3, 4}; int N = arr.Length; Console.Write(TotalXorPair(arr, N));}}// This code is contributed by Princi Singh |
4
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 ) = aEach 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++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to get bitwise XOR// of all possible pairs of// the given arrayint TotalXorPair(int arr[], int N){ // Stores bitwise XOR // of all possible pairs int totalXOR = 0; // Check if N is odd if (N % 2 != 0) { return 0; } // If N is even then calculate // bitwise XOR of all elements // of the given array. for (int i = 0; i < N; i++) { totalXOR ^= arr[i]; } return totalXOR;}// Driver Codeint main(){ int arr[] = { 1, 2, 3, 4 }; int N = sizeof(arr) / sizeof(arr[0]); cout << TotalXorPair(arr, N);} |
Java
// Java program to implement // the above approach class GFG{ // Function to get bitwise XOR // of all possible pairs of // the given array public static int TotalXorPair(int arr[], int N) { // Stores bitwise XOR // of all possible pairs int totalXOR = 0; // Check if N is odd if( N % 2 != 0 ) { return 0; } // If N is even then calculate // bitwise XOR of all elements // of the array for(int i = 0; i < N; i++) { totalXOR ^= arr[i]; } return totalXOR; } // Driver code public static void main(String[] args) { int arr[] = {1, 2, 3, 4}; int N = arr.length; System.out.print(TotalXorPair(arr, N));}}// This code is contributed by math_lover |
Python3
# Python3 program to implement# the above appraoch# Function to get bitwise XOR# of all possible pairs of# the given array def TotalXorPair(arr, N): # Stores bitwise XOR # of all possible pairs totalXOR = 0 # Check if N is odd if (N % 2 != 0): return 0 # If N is even then calculate # bitwise XOR of all elements # of the given array. for i in range(N): totalXOR ^= arr[i] return totalXOR# Driver codeif __name__ == '__main__': arr = [ 1, 2, 3, 4 ] N = len(arr) print(TotalXorPair(arr, N))# This code is contributed by Shivam Singh |
C#
// C# program to implement // the above approach using System;class GFG{ // Function to get bitwise XOR // of all possible pairs of // the given array static int TotalXorPair(int []arr, int N) { // Stores bitwise XOR // of all possible pairs int totalXOR = 0; // Check if N is odd if (N % 2 != 0) { return 0; } // If N is even then calculate // bitwise XOR of all elements // of the array for(int i = 0; i < N; i++) { totalXOR ^= arr[i]; } return totalXOR; } // Driver code public static void Main(String[] args) { int []arr = { 1, 2, 3, 4 }; int N = arr.Length; Console.Write(TotalXorPair(arr, N));}}// This code is contributed by doreamon_ |
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- XOR of pairwise sum of every unordered pairs in an array
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Find sum of xor of all unordered triplets of the array
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Count pairs having bitwise XOR greater than K from a given array
- Count pairs having Bitwise XOR less than K from given array
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Queries to count the number of unordered co-prime pairs from 1 to N
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Count pairs with equal Bitwise AND and Bitwise OR value
- Count pairs with Bitwise XOR as ODD number
- Count pairs with Bitwise XOR as EVEN number
- Sum of Bitwise And of all pairs in a given array
- Sum of Bitwise OR of all pairs in a given array
- Replace every element of the array with BitWise XOR of all other
- Construct the Array using given bitwise AND, OR and XOR
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.

