category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

477. Total Hamming Distance

class Solution {
  public:
    int totalHammingDistance(vector<int>& nums) {
      int ans = 0; 
      for (int i = 0; i < 32; i++) {
        int mask = (1 << i);

        int countZero = 0; 
        int countOne = 0;

        for (const auto& num: nums) 
          mask & num ? countOne ++ : countZero ++;

        ans += (countOne * countZero);
      }
      return ans;
    }
};