added bit-manupulation/total-hamming-distance

pull/1223/head
subhojeet chowdhury 2023-07-20 12:22:40 +05:30
parent d3c2184af8
commit 56ae1fee9e
1 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,37 @@
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int i,j,k,one,zero;
int result = 0;
for(i = 0; i < 32; i++){
k = 1 << i;
one = 0;
zero = 0;
for(auto value: nums){
if((value & k)) one++;
else zero++;
}
if(one == nums.size() || zero == nums.size()) continue;
result += one*zero;
}
return result;
}
};
int main(){
vector<int> nums{4,14,2};
Solution solution;
cout<<"Total hammind distance: ";
cout<<solution.totalHammingDistance(nums)<<endl;
return 0;
}