Advertisement
Kali_prasad

bits sum pairwise

Mar 16th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. int Solution::cntBits(vector<int> &A) {
  2.     int max=*max_element(A.begin(),A.end());
  3.     int size=A.size();
  4.     const unsigned int M = 1000000007;
  5.     long long int sum=0;
  6.     for(int i=0;i<=log2(max);i++)
  7.     {
  8.        int ones=0;
  9.         int zeros=0;
  10.         for(auto x:A)
  11.         {
  12.             if(x&(1<<i))
  13.                 ones++;
  14.             else
  15.                 zeros++;
  16.                          
  17.         }
  18.         sum%=M;
  19.         int z=(2*ones*zeros);
  20.    
  21.         sum+=z;
  22.         sum%=M;
  23.     }
  24.     return (sum)%M;
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement