Find sum of different corresponding bits for all pairs - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Wednesday 10 July 2019

Find sum of different corresponding bits for all pairs

We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2.
You are given an array of N integers, A1, A2 ,…, AN. Find sum of f(Ai, Aj) for all pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 109+7.
Input:
The first line of each input consists of the test cases. The description of T test cases is as follows:
The first line of each test case contains the size of the array, and the second line has the elements of the array.

Output:
In each separate line print sum of all pairs for (i, j) such that 1 ≤ i, j ≤ N and return theanswer modulo 109+7.


Input: arr[] = {1, 2}
Output: 4
All pairs in array are (1, 1), (1, 2)
                       (2, 1), (2, 2)
Sum of bit differences = 0 + 2 +
                         2 + 0
                      = 4

Input:  arr[] = {1, 3, 5}
Output: 8
All pairs in array are (1, 1), (1, 3), (1, 5)
                       (3, 1), (3, 3) (3, 5),
                       (5, 1), (5, 3), (5, 5)
Sum of bit differences =  0 + 1 + 1 +
                          1 + 0 + 2 +
                          1 + 2 + 0 
                       = 8
Simple Solution is to run two loops to consider all pairs one by one. For every pair, count bit differences. Finally return sum of counts. Time complexity of this solution is O(n2).
An Efficient Solution can solve this problem in O(n) time using the fact that all numbers are represented using 32 bits (or some fixed number of bits). The idea is to count differences at individual bit positions. We traverse from 0 to 31 and count numbers with i’th bit set. Let this count be ‘count’. There would be “n-count” numbers with i’th bit not set. So count of differences at i’th bit would be “count * (n-count) * 2”, the reason for this formula is as every pair having one element which has set bit at i’th position and second element having unset bit at i’th position contributes exactly 1 to sum, therefore total permutation count will be count*(n-count) and multiply by 2 is due to one more repetition of all this type of pair as per given condition for making pair 1<=i,j<=N.
#include  using namespace std;
int sumBitDifferences(int arr[], int n) { int ans = 0; // Initialize result
// traverse over all bits for (int i = 0; i < 32; i++) { 
// count number of elements with i'th bit set
 int count = 0; 
for (int j = 0; j < n; j++) 
     if ( (arr[j] & (1 << i)) ) 
             count++;
 // Add "count * (n - count) * 2" to the answer
 ans += (count * (n - count) * 2); 
return ans;     } 
// Driver prorgram 
int main() { 
int arr[] = {1, 3, 5};
 int n = sizeof arr / sizeof arr[0]; 
cout << sumBitDifferences(arr, n) << endl; 
return 0; } [