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 11
Explanation:
For Q[0] ( = 3): Sum = 5 ^ 3 + 2 ^ 3 + 3 ^ 3 = 7.
For Q[1] ( = 8): Sum = 5 ^ 8 + 2 ^ 8 + 3 ^ 8 = 34.
For Q[2] ( = 7): Sum = 5 ^ 7 + 2 ^ 7 + 3 ^ 7 = 11.
Input: arr[ ] = {2, 3, 4}, Q[ ] = {1, 2}
Output: 10 7
Naive Approach: The simplest approach to solve the problem is to traverse the array Q[] and for each array element, calculate the sum of its Bitwise XOR with all elements of the array arr[].
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: Follow the steps below to optimize the above approach:
- Initialize an array count[], of size 32. to store the count of set bits at each position of the elements of the array arr[].
- Traverse the array arr[].
- Update the array count[] accordingly. In a 32-bit binary representation, if the ith bit is set, increase the count of set bits at that position.
- Traverse the array Q[] and for each array element, perform the following operations:
- Initialize variables, say sum = 0, to store the required sum of Bitwise XOR .
- Iterate over each bit positions of the current element.
- If current bit is set, add count of elements with ith bit not set * 2i to sum.
- Otherwise, add count[i] * 2i.
- Finally, print the value of sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int xorSumOfArray( int arr[], int n, int k, int count[])
{
int sum = 0;
int p = 1;
for ( int i = 0; i < 31; i++) {
int val = 0;
if ((k & (1 << i)) != 0) {
int not_set = n - count[i];
val = ((not_set)*p);
}
else {
val = (count[i] * p);
}
sum += val;
p = (p * 2);
}
return sum;
}
void sumOfXors( int arr[], int n, int queries[], int q)
{
int count[32];
memset (count, 0, sizeof (count));
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < 31; j++) {
if (arr[i] & (1 << j))
count[j]++;
}
}
for ( int i = 0; i < q; i++) {
int k = queries[i];
cout << xorSumOfArray(arr, n, k, count) << " " ;
}
}
int main()
{
int arr[] = { 5, 2, 3 };
int queries[] = { 3, 8, 7 };
int n = sizeof (arr) / sizeof ( int );
int q = sizeof (queries) / sizeof ( int );
sumOfXors(arr, n, queries, q);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static int xorSumOfArray( int arr[], int n,
int k, int count[])
{
int sum = 0 ;
int p = 1 ;
for ( int i = 0 ; i < 31 ; i++)
{
int val = 0 ;
if ((k & ( 1 << i)) != 0 )
{
int not_set = n - count[i];
val = ((not_set)*p);
}
else
{
val = (count[i] * p);
}
sum += val;
p = (p * 2 );
}
return sum;
}
static void sumOfXors( int arr[], int n,
int queries[], int q)
{
int []count = new int [ 32 ];
Arrays.fill(count, 0 );
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < 31 ; j++)
{
if ((arr[i] & ( 1 << j)) != 0 )
count[j]++;
}
}
for ( int i = 0 ; i < q; i++)
{
int k = queries[i];
System.out.print(
xorSumOfArray(arr, n, k, count) + " " );
}
}
public static void main(String args[])
{
int arr[] = { 5 , 2 , 3 };
int queries[] = { 3 , 8 , 7 };
int n = arr.length;
int q = queries.length;
sumOfXors(arr, n, queries, q);
}
}
|
Python3
def xorSumOfArray(arr, n, k, count):
sum = 0
p = 1
for i in range ( 31 ):
val = 0
if ((k & ( 1 << i)) ! = 0 ):
not_set = n - count[i]
val = ((not_set) * p)
else :
val = (count[i] * p)
sum + = val
p = (p * 2 )
return sum
def sumOfXors(arr, n, queries, q):
count = [ 0 for i in range ( 32 )]
for i in range (n):
for j in range ( 31 ):
if (arr[i] & ( 1 << j)):
count[j] + = 1
for i in range (q):
k = queries[i]
print (xorSumOfArray(arr, n, k, count), end = " " )
if __name__ = = '__main__' :
arr = [ 5 , 2 , 3 ]
queries = [ 3 , 8 , 7 ]
n = len (arr)
q = len (queries)
sumOfXors(arr, n, queries, q)
|
C#
using System;
public class GFG{
static int xorSumOfArray( int []arr, int n, int k, int []count)
{
int sum = 0;
int p = 1;
for ( int i = 0; i < 31; i++) {
int val = 0;
if ((k & (1 << i)) != 0) {
int not_set = n - count[i];
val = ((not_set)*p);
}
else {
val = (count[i] * p);
}
sum += val;
p = (p * 2);
}
return sum;
}
static void sumOfXors( int []arr, int n, int []queries, int q)
{
int []count = new int [32];
for ( int i = 0; i < 32; i++)
count[i] = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < 31; j++) {
if ((arr[i] & (1 << j)) != 0)
count[j]++;
}
}
for ( int i = 0; i < q; i++) {
int k = queries[i];
Console.Write(xorSumOfArray(arr, n, k, count) + " " );
}
}
static public void Main ()
{
int []arr = { 5, 2, 3 };
int []queries = { 3, 8, 7 };
int n = arr.Length;
int q = queries.Length;
sumOfXors(arr, n, queries, q);
}
}
|
Javascript
<script>
function xorSumOfArray(arr, n, k, count)
{
var sum = 0;
var p = 1;
for ( var i = 0; i < 31; i++)
{
var val = 0;
if ((k & (1 << i)) != 0)
{
var not_set = n - count[i];
val = ((not_set)*p);
}
else
{
val = (count[i] * p);
}
sum += val;
p = (p * 2);
}
return sum;
}
function sumOfXors(arr, n, queries, q)
{
var count = new Array(32);
count.fill(0);
for ( var i = 0; i < n; i++)
{
for ( var j = 0; j < 31; j++)
{
if (arr[i] & (1 << j))
count[j]++;
}
}
for ( var i = 0; i < q; i++)
{
var k = queries[i];
document.write(xorSumOfArray(
arr, n, k, count) + " " );
}
}
var arr = [ 5, 2, 3 ];
var queries = [ 3, 8, 7 ];
var n = arr.length;
var q = queries.length;
sumOfXors(arr, n, queries, q);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...