Sum of bit differences among all pairs
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.
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).
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的更多相关文章
- 数论 - Pairs(数字对)
In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...
- 暑假练习赛 007 E - Pairs
E - Pairs Description standard input/outputStatements In the secret book of ACM, it’s said: “Glory f ...
- A. Difference Row
A. Difference Row time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- t检验,T Test (Student’s T-Test)
1.什么是T test? t-test:比较数据的均值,告诉你这两者之间是否相同,并给出这种不同的显著性(即是否是因为偶然导致的不同) The t test (also called Student’ ...
- 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 ...
- [C6] Andrew Ng - Convolutional Neural Networks
About this Course This course will teach you how to build convolutional neural networks and apply it ...
- codefroces Round #201.a--Difference Row
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description You wa ...
- [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 ...
- CodeForces Round 198
总体感觉这次出的题偏数学,数学若菜表示果断被虐.不过看起来由于大家都被虐我2题居然排到331,rating又升了74.Div2-AA. The Walltime limit per test1 sec ...
随机推荐
- Linux(Ubuntu 命令大全)
Ubuntu 一. Ubuntu简介 Ubuntu(乌班图)是一个基于Debian的以桌面应用为主的Linux操作系统,据说其名称来自非洲南部祖鲁语或科萨语的“ubuntu”一词,意思是“人性”.“我 ...
- Unresolved defparam reference to 'read_aclr_synch' in dcfifo_component.read_aclr_synch
问题: 今天用 questasim 仿真出现下面这个问题. Unresolved defparam reference to 'read_aclr_synch' in dcfifo_component ...
- Hadoop平台K-Means聚类算法分布式实现+MapReduce通俗讲解
Hadoop平台K-Means聚类算法分布式实现+MapReduce通俗讲解 在Hadoop分布式环境下实现K-Means聚类算法的伪代码如下: 输入:参数0--存储样本数据的文本文件inpu ...
- shell之route命令相关
使用下面的 route 命令可以查看 Linux 内核路由表. # route Destination Gateway Genmask Flags Metric Ref Use Iface 192.1 ...
- cfq调度器
cfq调度是block层最复杂的一个调度器,主要思想是是说每个进程平均享用IO带宽,实现方法是在时间上对进程进行划分,以此达到平均占用IO的目的.带着几个问题去看cfq 1)现在进程来了之后,是插入到 ...
- [洛谷P4151][WC2011]最大XOR和路径
题目大意:给你一张$n$个点$m$条边的无向图,求一条$1->n$的路径,使得经过路径值的异或值最大(重复经过重复计算) 题解:某条路$k$被重复走了两次,那么它的权值对答案的贡献就是$0$,但 ...
- BZOJ5297 [Cqoi2018]社交网络 【矩阵树定理】
题目链接 BZOJ5297 题解 最近这玩意这么那么火 这题要用到有向图的矩阵树定理 主对角线上对应入度 剩余位置如果有边则为\(-1\),不然为\(0\) \(M_{i,i}\)即为以\(i\)为根 ...
- What is a Microsoft Failover Cluster Virtual Adapter
What is a Microsoft Failover Cluster Virtual Adapter anyway? A question often asked is, "What i ...
- "贪心"的考试
先来落实比较简单的T3,T4 T3 团结的队伍 team 题目描述 众所周知,长郡信息组是一个团结友爱的优秀团队.为了弘扬团队精神,践行社会主义核心价值观,秉承朴实沉毅的校训,信息组决定外出游玩(. ...
- ScrollLayer
http://www.oschina.net/p/scrolllayer ScrollLayer 编辑/纠错 分享到: 已用 +1 收藏 +12 4月18日 武汉 源创会开始报名,送华为开 ...