#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;
}
Original page link
Best Cool Tech Gadgets
Top favorite technology gadgets
0 comments:
Post a Comment