Sum of XOR of all pairs in an array
Given an array of n integers, find the sum of xor of all pairs of numbers in the array.
Examples :
Input : arr[] = {7, 3, 5}
Output : 12
7 ^ 3 = 4
3 ^ 5 = 6
7 ^ 5 = 2
Sum = 4 + 6 + 2
= 12
Input : arr[] = {5, 9, 7, 6}
Output : 47
5 ^ 9 = 12
9 ^ 7 = 14
7 ^ 6 = 1
5 ^ 7 = 2
5 ^ 6 = 3
9 ^ 6 = 15
Sum = 12 + 14 + 1 + 2 + 3 + 15
= 47
Naive Solution
A Brute Force approach is to run two loops and time complexity is O(n2).
C++
// A Simple C++ program to compute // sum of bitwise OR of all pairs #include <bits/stdc++.h> using namespace std; // Returns sum of bitwise OR // of all pairs int pairORSum(int arr[], int n) { int ans = 0; // Initialize result // Consider all pairs (arr[i], arr[j) such that // i < j for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) ans += arr[i] ^ arr[j]; return ans; } // Driver program to test above function int main() { int arr[] = { 5, 9, 7, 6 }; int n = sizeof(arr) / sizeof(arr[0]); cout << pairORSum(arr, n) << endl; return 0; } |
Java
// A Simple Java program to compute // sum of bitwise OR of all pairs import java.io.*; class GFG { // Returns sum of bitwise OR // of all pairs static int pairORSum(int arr[], int n) { // Initialize result int ans = 0; // Consider all pairs (arr[i], arr[j) // such that i < j for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) ans += arr[i] ^ arr[j]; return ans; } // Driver program to test above function public static void main (String[] args) { int arr[] = { 5, 9, 7, 6 }; int n = arr.length; System.out.println(pairORSum(arr, arr.length)); } } // This code is contributed by vt_m |
Python3
# A Simple Python 3 program to compute # sum of bitwise OR of all pairs # Returns sum of bitwise OR # of all pairs def pairORSum(arr, n) : ans = 0 # Initialize result # Consider all pairs (arr[i], arr[j) # such that i < j for i in range(0, n) : for j in range(i + 1, n) : ans = ans + (arr[i] ^ arr[j]) return ans # Driver Code arr = [ 5, 9, 7, 6 ] n = len(arr) print(pairORSum(arr, n)) # This code is contributed by Nikita Tiwari. |
C#
// A Simple C# program to compute // sum of bitwise OR of all pairs using System; class GFG { // Returns sum of bitwise OR // of all pairs static int pairORSum(int []arr, int n) { // Initialize result int ans = 0; // Consider all pairs (arr[i], arr[j) // such that i < j for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) ans += arr[i] ^ arr[j]; return ans; } // Driver program to test above function public static void Main () { int []arr = { 5, 9, 7, 6 }; int n = arr.Length; Console.WriteLine(pairORSum(arr, arr.Length)); } } // This code is contributed by vt_m |
PHP
<?php // A Simple PHP program to compute // sum of bitwise OR of all pairs // Returns sum of bitwise OR // of all pairs function pairORSum($arr, $n) { // Initialize result $ans = 0; // Consider all pairs // (arr[i], arr[j) such that // i < j for ( $i = 0; $i < $n; $i++) for ( $j = $i + 1; $j < $n; $j++) $ans += $arr[$i] ^ $arr[$j]; return $ans; } // Driver Code $arr = array( 5, 9, 7, 6 ); $n = count($arr); echo pairORSum($arr, $n) ; // This code is contributed by anuj_67. ?> |
Output :
47
Efficient Solution
An Efficient Solution can solve this problem in O(n) time. The assumption here is that integers are represented using 32 bits.
Optimized solution will be to try bit manipulation. To implement the solution, we consider all bits which are 1 and which are 0 and store their count in two different variables. Next multiple those counts along with the power of 2 raised to that bit position. Do this for all the bit positions of the numbers. Their sum would be our answer.
Explanation : arr[] = { 7, 3, 5 }
7 = 1 1 1
3 = 0 1 1
5 = 1 0 1
For bit position 0 :
Bits with zero = 0
Bits with one = 3
Answer = 0 * 3 * 2 ^ 0 = 0
Similarly, for bit position 1 :
Bits with zero = 1
Bits with one = 2
Answer = 1 * 2 * 2 ^ 1 = 4
Similarly, for bit position 2 :
Bits with zero = 1
Bits with one = 2
Answer = 1 * 2 * 2 ^ 2 = 8
Final answer = 0 + 4 + 8 = 12
CPP
// An efficient C++ program to compute // sum of bitwise OR of all pairs #include <bits/stdc++.h> using namespace std; // Returns sum of bitwise OR // of all pairs long long int sumXOR(int arr[], int n) { long long int sum = 0; for (int i = 0; i < 32; i++) { // Count of zeros and ones int zc = 0, oc = 0; // Individual sum at each bit position long long int idsum = 0; for (int j = 0; j < n; j++) { if (arr[j] % 2 == 0) zc++; else oc++; arr[j] /= 2; } // calculating individual bit sum idsum = oc * zc * (1 << i); // final sum sum += idsum; } return sum; } int main() { long long int sum = 0; int arr[] = { 5, 9, 7, 6 }; int n = sizeof(arr) / sizeof(arr[0]); sum = sumXOR(arr, n); cout << sum; return 0; } |
Java
// An efficient Java program to compute // sum of bitwise OR of all pairs import java.io.*; class GFG { // Returns sum of bitwise OR // of all pairs static long sumXOR(int arr[], int n) { long sum = 0; for (int i = 0; i < 32; i++) { // Count of zeros and ones int zc = 0, oc = 0; // Individual sum at each bit position long idsum = 0; for (int j = 0; j < n; j++) { if (arr[j] % 2 == 0) zc++; else oc++; arr[j] /= 2; } // calculating individual bit sum idsum = oc * zc * (1 << i); // final sum sum += idsum; } return sum; } // Driver Code public static void main(String args[]) { long sum = 0; int arr[] = { 5, 9, 7, 6 }; int n = arr.length; sum = sumXOR(arr, n); System.out.println(sum); } } // This code is contributed by Nikita Tiwari. |
Python3
# An efficient Python3 program to compute # sum of bitwise OR of all pair # Returns sum of bitwise OR # of all pairs def sumXOR( arr, n): sum = 0 for i in range(0, 32): # Count of zeros and ones zc = 0 oc = 0 # Individual sum at each bit position idsum = 0 for j in range(0, n): if (arr[j] % 2 == 0): zc = zc + 1 else: oc = oc + 1 arr[j] = int(arr[j] / 2) # calculating individual bit sum idsum = oc * zc * (1 << i) # final sum sum = sum + idsum; return sum # driver function sum = 0arr = [ 5, 9, 7, 6 ] n = len(arr) sum = sumXOR(arr, n); print (sum) # This code is contributed by saloni1297 |
C#
// An efficient C# program to compute // sum of bitwise OR of all pairs using System; class GFG { // Returns sum of bitwise OR // of all pairs static long sumXOR(int []arr, int n) { long sum = 0; for (int i = 0; i < 32; i++) { // Count of zeros and ones int zc = 0, oc = 0; // Individual sum at each bit position long idsum = 0; for (int j = 0; j < n; j++) { if (arr[j] % 2 == 0) zc++; else oc++; arr[j] /= 2; } // calculating individual bit sum idsum = oc * zc * (1 << i); // final sum sum += idsum; } return sum; } // Driver Code public static void Main() { long sum = 0; int []arr = { 5, 9, 7, 6 }; int n = arr.Length; sum = sumXOR(arr, n); Console.WriteLine(sum); } } // This code is contributed by vt_m. |
PHP
<?php // An efficient PHP program to compute // sum of bitwise OR of all pairs // Returns sum of bitwise OR // of all pairs function sumXOR($arr, $n) { $sum = 0; for ($i = 0; $i < 32; $i++) { // Count of zeros and ones $zc = 0; $oc = 0; // Individual sum at each // bit position $idsum = 0; for ($j = 0; $j < $n; $j++) { if ($arr[$j] % 2 == 0) $zc++; else $oc++; $arr[$j] /= 2; } // calculating individual bit sum $idsum = $oc * $zc * (1 << $i); // final sum $sum += $idsum; } return $sum; } // Driver code $sum = 0; $arr = array( 5, 9, 7, 6 ); $n = count($arr); $sum = sumXOR($arr, $n); echo $sum; // This code is contributed by anuj_67 ?> |
Output:
47
Recommended Posts:
- Find the maximum cost of an array of pairs choosing at most K pairs
- Find pairs in array whose sums already exist in array
- Sum of XOR of sum of all pairs in an array
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Sum of Bitwise OR of all pairs in a given array
- Sum of Bitwise And of all pairs in a given array
- Find all pairs (a, b) in an array such that a % b = k
- Sum of f(a[i], a[j]) over all pairs in an array of n integers
- Count of pairs (x, y) in an array such that x < y
- Count pairs in array whose sum is divisible by 4
- Count pairs in an array that hold i*arr[i] > j*arr[j]
- Number of recycled pairs in an array
- Count pairs in a sorted array whose sum is less than x
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd
- Number of prime pairs in an array
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.
Improved By : vt_m



