Sum of bit differences among all pairs
2015-11-14 05:41
441 查看
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:
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.
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 = 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; }
相关文章推荐
- Leetcode Swap Nodes in Pairs
- 使用nvm做nodejs的版本管理
- freemarker、jsp、velocity
- 基于HTML5的WebGL设计汉诺塔3D游戏
- 基于HTML5的WebGL设计汉诺塔3D游戏
- css命名规则
- css样式实现背景图片自适应
- CSS之Position详解
- Css自动截取字符串超过的部分
- LeetCode19:Remove Nth Node From End of List
- CSS渐变
- 02JsDom--获取页面元素
- JavaScript 字符串和日期内容整理
- 01JsDom--操作DOM的意义
- 使用jsp:include控制动态内容的方法
- javascript动态生成树形菜单的方法
- js读取并解析JSON类型数据的方法
- JS使用eval解析JSON的注意事项分析
- hive-1.1.0-cdh5.4.0 安装
- Zepto源码分析(1)——类型判断函数