
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sum of XOR of all pairs in an array in C++
In this problem, we are given an array arr[] of n integers. Our task is to create a program to find the sum of XOR of all pairs in an array.
Let’s take an example to understand the problem,
Input: arr[] = {5, 1, 4} Output: 10 Explanation: the sum of all pairs: 5 ^ 1 = 4 1 ^ 4 = 5 5 ^ 4 = 1 sum = 4 + 5 + 1 = 10
One simple approach to solve this problem is to run nested loops and find all pairs of numbers. Find XOR of each pair and add them to the sum.
Algorithm
Initialise sum = 0 Step 1: for(i -> 0 to n). Do: Step 1.1: for( j -> i to n ). Do: Step 1.1.1: update sum. i.e. sum += arr[i] ^ arr[j]. Step 2: return sum.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int findXORSum(int arr[], int n) { int sum = 0; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) sum += (arr[i]^arr[j]); return sum; } int main() { int arr[] = { 5, 1, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"Sum of XOR of all pairs in an array is "<<findXORSum(arr, n); return 0; }
Output
Sum of XOR of all pairs in an array is 10
The time complexity of the algorithm is O(n2) and is not the most efficient solution to the problem.
An efficient solution to the problem is using the bit manipulation technique.
Here, we will consider bits of the number and at each position. And apply the below formula find the intermediate sum,
(number of set bits) * (number of unset bits) * (2^(bit_position))
To find the final sum, we will add the intermediate sum of all bits.
Our solution is for the 64-bit integer value. For this approach, we need the number of bits.
Algorithm
Initialize sum = 0, setBits = 0, unsetBits = 0. Step 1: Loop for i -> 0 to 64. repeat steps 2, 3. Step 2: Reset setBits and unsetBits to 0. Step 3: For each element of the array find the value of setBits and unsetBits. At ith position. Step 4: update sum += (setBits * unsetBits * (2i))
Example
Program to illustrate the working of our solution,
#include <iostream> #include <math.h> using namespace std; long findXORSum(int arr[], int n) { long sum = 0; int unsetBits = 0, setBits = 0; for (int i = 0; i < 32; i++) { unsetBits = 0; setBits = 0; for (int j = 0; j < n; j++) { if (arr[j] % 2 == 0) unsetBits++; else setBits++; arr[j] /= 2; } sum += ( unsetBits*setBits* (pow(2,i)) ); } return sum; } int main() { int arr[] = { 5, 1, 4, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); cout<<"Sum of XOR of all pairs in an array is "<<findXORSum(arr, n); return 0; }
Output
Sum of XOR of all pairs in an array is 68
- Related Articles
- Sum of XOR of sum of all pairs in an array in C++
- Program to find XOR sum of all pairs bitwise AND in Python
- XOR of all Prime numbers in an Array in C++
- Sum of XOR of all subarrays in C++
- XOR of Sum of every possible pair of an array in C++
- Print all pairs in an unsorted array with equal sum in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Sum of XOR of all possible subsets in C++
- Maximum value of XOR among all triplets of an array in C++
- Minimizing array sum by applying XOR operation on all elements of the array in C++
- Count all pairs with given XOR in C++
- Find number of pairs in an array such that their XOR is 0 using C++.
- Construct an array from XOR of all elements of array except element at same index in C++
- Sum of all prime numbers in an array - JavaScript
- Sum of all positives present in an array in JavaScript
