Sum of Bitwise XOR of each array element with all other array elements
Last Updated :
09 Apr, 2021
Given an array arr[] of length N, the task for every array element is to print the sum of its Bitwise XOR with all other array elements.
Examples:
Input: arr[] = {1, 2, 3}
Output: 5 4 3
Explanation:
For arr[0]: arr[0] ^ arr[0] + arr[0] ^ arr[1] + arr[0] ^ arr[2] = 1^1 + 1^2 + 1^3 = 0 + 3 + 2 = 5
For arr[1]: arr[1] ^ arr[0] + arr[1] ^ arr[1] + arr[1] ^ arr[2] = 2^1 + 2^2 + 2^3 = 3 + 0 + 1 = 4
For arr[2]: arr[2] ^ arr[0] + arr[2] ^ arr[1] + arr[2] ^ arr[2] = 3^1 + 3^2 + 3^3 = 2 + 2 + 0 = 3
Input : arr[] = {2, 4, 8}
Output: 16 18 22
Explanation:
For arr[0]: arr[0] ^ arr[0] + arr[0] ^ arr[1] + arr[0] ^ arr[2] = 2^2 + 2^4 + 2^8 = 0 + 6 + 10 = 16.
For arr[1]: arr[1] ^ arr[0] + arr[1] ^ arr[1] + arr[1] ^ arr[2] = 4^2 + 4^4 + 4^8 = 6 + 0 + 12 = 18
For arr[2]: arr[2] ^ arr[0] + arr[2] ^ arr[1] + arr[2] ^ arr[2] = 8^2 + 8^4 + 8^8 = 10 + 12 + 0 = 22
Naive Approach: The idea is to traverse the array and for each array element, traverse the array and calculate sum of its Bitwise XOR with all other array elements.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: To` optimize the above approach, the idea is to use property of Bitwise XOR that similar bits on xor, gives 0, or 1 otherwise. Follow the below steps to solve the problem:
- Calculate the frequency of set bits at position i where 0 <= i <= 32, across all the elements of the array in a frequency array.
- For every element X, of the array, calculate the xor sum by running a loop from i=0 to 32 and check if ith bit of X is set.
- If yes, then add (N - frequency[i])*2i to the xor sum because the set bit of X at this position will make all the set bits to zero and all the unset bits to 1.
- Otherwise, add frequency[i] * 2i to the xor sum.
- Calculate the sum of all the xor sum of every element of the array and return as the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate for each array
// element, sum of its Bitwise XOR with
// all other array elements
void XOR_for_every_i(int A[], int N)
{
// Declare an array of size 64
// to store count of each bit
int frequency_of_bits[32]{};
// Traversing the array
for (int i = 0; i < N; i++) {
int bit_position = 0;
int M = A[i];
while (M) {
// Check if bit is present of not
if (M & 1) {
frequency_of_bits[bit_position] += 1;
}
// Increase the bit position
bit_position += 1;
// Reduce the number to half
M >>= 1;
}
}
// Traverse the array
for (int i = 0; i < N; i++) {
int M = A[i];
// Stores the bit position
int value_at_that_bit = 1;
// Stores the sum of Bitwise XOR
int XOR_sum = 0;
for (int bit_position = 0;
bit_position < 32;
bit_position++) {
// Check if bit is present of not
if (M & 1) {
XOR_sum
+= (N
- frequency_of_bits[bit_position])
* value_at_that_bit;
}
else {
XOR_sum
+= (frequency_of_bits[bit_position])
* value_at_that_bit;
}
// Reduce the number to its half
M >>= 1;
value_at_that_bit <<= 1;
}
// Print the sum for A[i]
cout << XOR_sum << ' ';
}
return;
}
// Driver Code
int main()
{
// Given array
int A[] = { 1, 2, 3 };
// Given N
int N = sizeof(A) / sizeof(A[0]);
// Function Call
XOR_for_every_i(A, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to calculate for each array
// element, sum of its Bitwise XOR with
// all other array elements
static void XOR_for_every_i(int A[], int N)
{
// Declare an array of size 64
// to store count of each bit
int frequency_of_bits[] = new int[32];
// Traversing the array
for(int i = 0; i < N; i++)
{
int bit_position = 0;
int M = A[i];
while (M != 0)
{
// Check if bit is present of not
if ((M & 1) != 0)
{
frequency_of_bits[bit_position] += 1;
}
// Increase the bit position
bit_position += 1;
// Reduce the number to half
M >>= 1;
}
}
// Traverse the array
for(int i = 0; i < N; i++)
{
int M = A[i];
// Stores the bit position
int value_at_that_bit = 1;
// Stores the sum of Bitwise XOR
int XOR_sum = 0;
for(int bit_position = 0;
bit_position < 32;
bit_position++)
{
// Check if bit is present of not
if ((M & 1) != 0)
{
XOR_sum += (N -
frequency_of_bits[bit_position]) *
value_at_that_bit;
}
else
{
XOR_sum += (frequency_of_bits[bit_position]) *
value_at_that_bit;
}
// Reduce the number to its half
M >>= 1;
value_at_that_bit <<= 1;
}
// Print the sum for A[i]
System.out.print( XOR_sum + " ");
}
return;
}
// Driver code
public static void main(String[] args)
{
// Given array
int A[] = { 1, 2, 3 };
// Given N
int N = A.length;
// Function Call
XOR_for_every_i(A, N);
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program for the above approach
# Function to calculate for each array
# element, sum of its Bitwise XOR with
# all other array elements
def XOR_for_every_i(A, N):
# Declare an array of size 64
# to store count of each bit
frequency_of_bits = [0] * 32
# Traversing the array
for i in range(N):
bit_position = 0
M = A[i]
while (M):
# Check if bit is present of not
if (M & 1 != 0):
frequency_of_bits[bit_position] += 1
# Increase the bit position
bit_position += 1
# Reduce the number to half
M >>= 1
# Traverse the array
for i in range(N):
M = A[i]
# Stores the bit position
value_at_that_bit = 1
# Stores the sum of Bitwise XOR
XOR_sum = 0
for bit_position in range(32):
# Check if bit is present of not
if (M & 1 != 0):
XOR_sum += ((N - frequency_of_bits[bit_position]) *
value_at_that_bit)
else:
XOR_sum += ((frequency_of_bits[bit_position]) *
value_at_that_bit)
# Reduce the number to its half
M >>= 1
value_at_that_bit <<= 1
# Print the sum for A[i]
print(XOR_sum, end = " ")
return
# Driver Code
# Given arr1[]
A = [ 1, 2, 3 ]
# Size of N
N = len(A)
# Function Call
XOR_for_every_i(A, N)
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
class GFG
{
// Function to calculate for each array
// element, sum of its Bitwise XOR with
// all other array elements
static void XOR_for_every_i(int[] A, int N)
{
// Declare an array of size 64
// to store count of each bit
int[] frequency_of_bits = new int[32];
// Traversing the array
for(int i = 0; i < N; i++)
{
int bit_position = 0;
int M = A[i];
while (M != 0)
{
// Check if bit is present of not
if ((M & 1) != 0)
{
frequency_of_bits[bit_position] += 1;
}
// Increase the bit position
bit_position += 1;
// Reduce the number to half
M >>= 1;
}
}
// Traverse the array
for(int i = 0; i < N; i++)
{
int M = A[i];
// Stores the bit position
int value_at_that_bit = 1;
// Stores the sum of Bitwise XOR
int XOR_sum = 0;
for(int bit_position = 0;
bit_position < 32;
bit_position++)
{
// Check if bit is present of not
if ((M & 1) != 0)
{
XOR_sum += (N -
frequency_of_bits[bit_position]) *
value_at_that_bit;
}
else
{
XOR_sum += (frequency_of_bits[bit_position]) *
value_at_that_bit;
}
// Reduce the number to its half
M >>= 1;
value_at_that_bit <<= 1;
}
// Print the sum for A[i]
Console.Write( XOR_sum + " ");
}
return;
}
// Driver Code
public static void Main()
{
// Given array
int[] A = { 1, 2, 3 };
// Given N
int N = A.Length;
// Function Call
XOR_for_every_i(A, N);
}
}
// This code is contributed by sanjoy_62
JavaScript
<script>
// JavaScript program for the above approach
// Function to calculate for each array
// element, sum of its Bitwise XOR with
// all other array elements
function XOR_for_every_i(A, N)
{
// Declare an array of size 64
// to store count of each bit
let frequency_of_bits = new Uint8Array(32);
// Traversing the array
for(let i = 0; i < N; i++)
{
let bit_position = 0;
let M = A[i];
while (M != 0)
{
// Check if bit is present of not
if ((M & 1) != 0)
{
frequency_of_bits[bit_position] += 1;
}
// Increase the bit position
bit_position += 1;
// Reduce the number to half
M >>= 1;
}
}
// Traverse the array
for(let i = 0; i < N; i++)
{
let M = A[i];
// Stores the bit position
let value_at_that_bit = 1;
// Stores the sum of Bitwise XOR
let XOR_sum = 0;
for(let bit_position = 0;
bit_position < 32;
bit_position++)
{
// Check if bit is present of not
if ((M & 1) != 0)
{
XOR_sum += (N -
frequency_of_bits[bit_position]) *
value_at_that_bit;
}
else
{
XOR_sum += (frequency_of_bits[bit_position]) *
value_at_that_bit;
}
// Reduce the number to its half
M >>= 1;
value_at_that_bit <<= 1;
}
// Print the sum for A[i]
document.write( XOR_sum + " ");
}
return;
}
// Driver code
// Given array
let A = [ 1, 2, 3 ];
// Given N
let N = A.length;
// Function Call
XOR_for_every_i(A, N);
// This code is contributed by Surbhi Tyagi.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Sum of Bitwise XOR of elements of an array with all elements of another array Given an array arr[] of size N and an array Q[], the task is to calculate the sum of Bitwise XOR of all elements of the array arr[] with each element of the array q[]. Examples: Input: arr[ ] = {5, 2, 3}, Q[ ] = {3, 8, 7}Output: 7 34 11Explanation:For Q[0] ( = 3): Sum = 5 ^ 3 + 2 ^ 3 + 3 ^ 3 = 7.For
9 min read
Sum of Bitwise OR of every array element paired with all other array elements Given an array arr[] consisting of non-negative integers, the task for each array element arr[i] is to print the sum of Bitwise OR of all pairs (arr[i], arr[j]) ( 0 ⤠j ⤠N ). Examples: Input: arr[] = {1, 2, 3, 4}Output: 12 14 16 22Explanation:For i = 0 the required sum will be (1 | 1) + (1 | 2) + (
11 min read
Sum of Bitwise AND of each array element with the elements of another array Given two arrays arr1[] of size M and arr2[] of size N, the task is to find the sum of bitwise AND of each element of arr1[] with the elements of the array arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {1, 2, 3}, M = 3, N = 3Output: 2 4 6Explanation:For elements at index 0 in arr1[], Sum = a
11 min read
Sum of Bitwise OR of each array element of an array with all elements of another array Given two arrays arr1[] of size M and arr2[] of size N, the task is to find the sum of bitwise OR of each element of arr1[] with every element of the array arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {1, 2, 3}, M = 3, N = 3Output: 7 8 9Explanation: For arr[0]: Sum = arr1[0]|arr2[0] + arr1[
11 min read
Replace every element of the array with BitWise XOR of all other Given an array of integers. The task is to replace every element by the bitwise xor of all other elements of the array. Examples: Input: arr[] = { 2, 3, 3, 5, 5 } Output: 0 1 1 7 7 Bitwise Xor of 3, 3, 5, 5 = 0 Bitwise Xor of 2, 3, 5, 5 = 1 Bitwise Xor of 2, 3, 5, 5 = 1 Bitwise Xor of 2, 3, 3, 5 = 7
5 min read