Sum of bit differences among all pairs - GeeksforGeeks
Given an integer array of n integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y.
For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 ( first and last bits differ in two numbers).
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″.
http://code.geeksforgeeks.org/Nl9IS1
private static void sumOfBitDiffAmongAllPairs(int[] arr) {
int sum = 0;
for(int i = 0;i<arr.length;i++) {
for(int j = 0;j<arr.length;j++) {
if(arr[i] != arr[j]) { //diff is zero for pair with same numbers, hence ignore
int xor = arr[i] ^ arr[j]; // xor gives the difference between two numbers
while(xor>0) { // setbits for the difference between two numbers
xor&=xor-1;
sum++;
}
}
}
}
System.out.println(sum);
}
}
Read full article from Sum of bit differences among all pairs - GeeksforGeeks
Given an integer array of n integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y.
For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 ( first and last bits differ in two numbers).
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″.
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;
}
A 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).
private static void sumOfBitDiffAmongAllPairs(int[] arr) {
int sum = 0;
for(int i = 0;i<arr.length;i++) {
for(int j = 0;j<arr.length;j++) {
if(arr[i] != arr[j]) { //diff is zero for pair with same numbers, hence ignore
int xor = arr[i] ^ arr[j]; // xor gives the difference between two numbers
while(xor>0) { // setbits for the difference between two numbers
xor&=xor-1;
sum++;
}
}
}
}
System.out.println(sum);
}
}
Read full article from Sum of bit differences among all pairs - GeeksforGeeks