Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Bit Manipulation but will also help you build up problem-solving skills.
POTD 30 October: Sum of XOR of all pairs
Given an array of N integers, find the sum of xor of all pairs of numbers in the array arr. In other words, select all possible pairs of i and j from 0 to N-1 (i<j) and determine sum of all (arri xor arrj).
Example 1:
Input: arr[] = {7, 3, 5}
Output: 12
Explanation: All possible pairs and sum of their XOR Values: (3 ^ 5) + (7 ^ 3) + (7 ^ 5) = 6 + 4 + 2 = 12Example 2:
Input: {5, 9, 7, 6}
Output: 47
Explanation: All possible pairs and sum of their XOR Values: (5 ^ 9) + (5 ^ 7) + (5 ^ 6) + (9 ^ 7) + (9 ^ 6) + (7 ^ 6) = 12 + 2 + 3 + 14 + 15 + 1 = 47
Sum of XOR of all pairs using Bit Manipulation:
The idea is that for every bit position i, we consider all bits which are 1 and which are 0 and store their count in two different variables. Next multiply those counts along with the power of 2 raised to that bit position i. Do this for all the bit positions of the numbers. Their sum would be our answer.
For any bit position i, suppose A numbers have their ith bit unset (ith bit = 0) and B numbers have their ith bit set (ith bit = 1). Then out of all the pairs, A*B pairs will have the ith bit set in their XOR. This is because there are A*B ways to choose one number that has a 0-bit and one that has a 1-bit. These bits will therefore contribute A*B towards the total of all the XORs. The contribution towards the final sum will be A*B*pow(2,i). Do this for each bit and sum all these contributions together.
Below is the implementation of the above approach:
class Solution {
public :
// Returns sum of bitwise OR
// of all pairs
long long int sumXOR( int arr[], int n)
{
// Complete the function
long long int sum = 0;
for ( int i = 0; i < 32; i++) {
// Count of zeros and ones
int A = 0, B = 0;
// Individual sum at each bit position
long long int idsum = 0;
for ( int j = 0; j < n; j++) {
if (arr[j] % 2 == 0)
A++;
else
B++;
arr[j] /= 2;
}
// calculating individual bit sum
idsum = A * 1LL * B * (1LL << i);
// final sum
sum += idsum;
}
return sum;
}
}; |
class Solution {
// Function for finding maximum and value pair
public long sumXOR( int arr[], int n)
{
// Complete the function
long sum = 0 ;
for ( int i = 0 ; i < 32 ; i++) {
// Count of zeros and ones
long A = 0 , B = 0 ;
// Individual sum at each bit position
long idsum = 0 ;
for ( int j = 0 ; j < n; j++) {
if (arr[j] % 2 == 0 )
A++;
else
B++;
arr[j] /= 2 ;
}
// calculating individual bit sum
idsum = A * B * ( 1 << i);
// final sum
sum += idsum;
}
return sum;
}
} |
class Solution:
def sumXOR( self , arr, n):
sum = 0
for i in range ( 0 , 32 ):
# Count of zeros and ones
A = 0
B = 0
# Individual sum at each bit position
idsum = 0
for j in range ( 0 , n):
if (arr[j] % 2 = = 0 ):
A = A + 1
else :
B = B + 1
arr[j] = arr[j] / / 2
# calculating individual bit sum
idsum = A * B * ( 1 << i)
# final sum
sum = sum + idsum
return sum
|
Time Complexity: O(K * N), where K is the number of bits in the input array and N is the size of array
Auxiliary Space: O(1)