zoukankan      html  css  js  c++  java
  • 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.

    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.

     1 // C++ program to compute sum of pairwise bit differences
     2 #include <bits/stdc++.h>
     3 using namespace std;
     4  
     5 int sumBitDifferences(int arr[], int n)
     6 {
     7     int ans = 0;  // Initialize result
     8  
     9     // traverse over all bits
    10     for (int i = 0; i < 32; i++)
    11     {
    12         // count number of elements with i'th bit set
    13         int count = 0;
    14         for (int j = 0; j < n; j++)
    15             if ( (arr[j] & (1 << i)) )
    16                 count++;
    17  
    18         // Add "count * (n - count) * 2" to the answer
    19         ans += (count * (n - count) * 2);
    20     }
    21  
    22     return ans;
    23 }
    24  
    25 // Driver prorgram
    26 int main()
    27 {
    28     int arr[] = {1, 3, 5};
    29     int n = sizeof arr / sizeof arr[0];
    30     cout << sumBitDifferences(arr, n) << endl;
    31     return 0;
    32 }
  • 相关阅读:
    LODOP中用ADD_PRINT_IMAGE缩放非图片超文本
    LODOP关联,打印项序号注意事项
    LODOP在页面让客户选择打印机
    【JS新手教程】LODOP打印复选框选中的任务或页数
    映美FP-530K+打印发票的各种经验
    【JS新手教程】LODOP打印复选框选中的内容
    LODOP和C-LODOP注册与角色等简短问答【增强版】
    【JS新手教程】弹出两层div,及在LODOP内嵌上层
    LODOP内嵌挡住浏览器的div弹出层
    【JS新手教程】浏览器弹出div层1
  • 原文地址:https://www.cnblogs.com/fenshen371/p/4963788.html
Copyright © 2011-2022 走看看