This article was found from Geeksforgeeks.org.

Click here to see the original article.

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).

Examples:

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

Source: Google Interview Question

We strongly recommend you to minimize your browser and try this yourself first.

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″.

Below is C++ implementation of above idea.

 // C++ program to compute sum of pairwise bit differences
#include <bits/stdc++.h>
using namespace std; int sumBitDifferences(int arr[], int n)
{
int ans = ; // Initialize result // traverse over all bits
for (int i = ; i < ; i++)
{
// count number of elements with i'th bit set
int count = ;
for (int j = ; j < n; j++)
if ( (arr[j] & ( << i)) )
count++; // Add "count * (n - count) * 2" to the answer
ans += (count * (n - count) * );
} return ans;
} // Driver prorgram
int main()
{
int arr[] = {, , };
int n = sizeof arr / sizeof arr[];
cout << sumBitDifferences(arr, n) << endl;
return ;
}

Sum of bit differences among all pairs的更多相关文章

  1. 数论 - Pairs(数字对)

    In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...

  2. 暑假练习赛 007 E - Pairs

    E - Pairs Description standard input/outputStatements In the secret book of ACM, it’s said: “Glory f ...

  3. A. Difference Row

    A. Difference Row time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. t检验,T Test (Student’s T-Test)

    1.什么是T test? t-test:比较数据的均值,告诉你这两者之间是否相同,并给出这种不同的显著性(即是否是因为偶然导致的不同) The t test (also called Student’ ...

  5. Spoj-BITDIFF Bit Difference

    Given an integer array of N integers, find the sum of bit differences in all the pairs that can be f ...

  6. [C6] Andrew Ng - Convolutional Neural Networks

    About this Course This course will teach you how to build convolutional neural networks and apply it ...

  7. codefroces Round #201.a--Difference Row

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description You wa ...

  8. [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  9. CodeForces Round 198

    总体感觉这次出的题偏数学,数学若菜表示果断被虐.不过看起来由于大家都被虐我2题居然排到331,rating又升了74.Div2-AA. The Walltime limit per test1 sec ...

随机推荐

  1. Jetty 安装、启动与项目部署

    Jetty是当下非常流行的一款轻量级Java Web服务器和Servlet容器实现,它由Eclipse基金会托管,完全免费而且开放源代码,因此所有人均可以从其官网下载最新源代码进行研究.由于其轻量.灵 ...

  2. python小结

    c:\python33添加到你的PATH 环境变量中,你可以在DOS 窗口中 输入以下命令:set path=%path%;C:\python33 id() 方法的返回值就是对象的内存地址. 在#! ...

  3. HDU 3954 Level up (线段树特殊懒惰标记)

    http://blog.csdn.net/acm_cxlove/article/details/7548087 感觉最巧的是定义了min_dis……将区间内有无英雄升级分开处理 #include &l ...

  4. 深入Spring Boot:ClassLoader的继承关系和影响

    前言 对spring boot本身启动原理的分析, Spring boot里的ClassLoader继承关系 可以运行下面提供的demo,分别在不同的场景下运行,可以知道不同场景下的Spring bo ...

  5. 《R语言实战》读书笔记--为什么要学

    本人最近在某咨询公司实习,涉及到了一些数据分析的工作,用的是R语言来处理数据.但是在应用的过程中,发现用R很不熟练,所以再打算学一遍R.曾经花一个月的时间看过一遍<R语言编程艺术>,还用R ...

  6. Three Garlands~Educational Codeforces Round 35

    C. Three Garlands time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. code forces 996BWorld Cup

    B. World Cup time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  8. 简易web服务器(npm)

    npm install -g http-server 以后可以在任何一个文件夹启动静态文件的访问通过http-server -a localhost -p 8000ctrl + c结束 http-se ...

  9. Bzoj1407 Savage

    Description Input 第1行为一个整数N(1<=N<=15),即 野人的数目.第2行到第N+1每行为三个整数Ci, Pi, Li (1<=Ci,Pi<=100, ...

  10. SqliteDeveloper 破解

    方法1: 注册表删除 HKEY_CURRENT_USER\SharpPlus\SqliteDev 下的 StartDate 方法2:  安装完成SQLite Developer后,先不要打开软件,下面 ...