Sum of bit differences | Practice | 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).
Note: (x, y) and (y, x) are considered two separate pairs.
Example 1:
Input: 
N = 2
arr[] = {1, 2}
Output: 4
Explanation: All pairs in array are (1, 1)
(1, 2), 2, 1), (2, 2)
Sum of bit differences = 0 + 2 + 2 + 0
                       = 4
Example 2:
Input:
N = 3 
arr[] = {1, 3, 5}
Output: 8
Explanation: 
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
Your Task:  
You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 105
1 <= arr[i] <= 105
 
0
guptatushar110412 weeks ago
C++ solution:-
long long sumBitDifferences(int arr[], int n) {
	    // code here
	    
	    long long  ans=0;
	    for(int i=0;i<31;i++)
	    {
	        long long count=0;
	        for(int j=0;j<n;j++)
	        {
	            if(arr[j] & (1<<i))
	            
	                count++;
	        }
	        ans=(ans+count*(n-count)*2);
	    }
	    return ans;
	}
0
keshavkumarshivanshu31 month ago
idea is very simple. for each position of every number check count of ones and zeros. so total ones*zeros combination of pair is possible so that it has difference.  
 
at last return double of answer because you have consider (a,b) and (b,a) differently
 
	long long sumBitDifferences(int arr[], int n) {
	    long long ans = 0;
	    for(int i=0;i<32;i++){
	        long long ones = 0;
	        for(int j=0;j<n;j++){
	            if(arr[j]&(1<<i)) ones++;
	        }
	        ans += (ones)*(n-ones);
	    }
	    return 2*ans;
	}
0
tiwariravikant20012 months ago
 long long sumBitDifferences(int a[], int n) {    long long int mask,i,k,unset,set=0,diff,res;       long long int ans=0;       for(i=0;i<32;i++)       {           mask=1<<i;           for(k=0;k<n;k++)           {               res=a[k]&mask;               if(res!=0)               ++set;           }           unset=n-set;           diff=2*unset*set;           ans=(ans+diff);           set=0;       }       return ans;}
0
kake13375 months ago
    int m=log2(*max_element(arr,arr+n));
    vector<int> b(m+1,0);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=m;j++)
        {
            int x=(1<<j);
            if((arr[i]&x)==x)
            b[j]++;
        }
    }
    long long r=0;
    for(int i=0;i<n;i++)
    {
        int x=1;
        for(int j=0;j<=m;j++)
        {
            if((arr[i]&x)==x)
            r+=(n-1)-(b[j]-1);
            else
            r+=b[j];
            x*=2;
        }
    }
    return r;
0
chessnoobdj7 months ago
C++
long long sumBitDifferences(int arr[], int n) {
	    long long int ans = 0;
	    for(int j=0; j<32; j++){
	        int cnt = 0;
	        for(int i=0; i<n; i++)
	            cnt += 1&(arr[i]>>j);
	        ans += (cnt)*(n-cnt);
	    }
	    return 2*ans;
	}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.