Can someone please explain how Hamington distance of [2,4,6] is 8?


#1

2 = 10
4 = 100
6 = 110

2^4 = 110
2 ^ 6 = 010
4 ^ 6 = 100

Distance = 4??


#2

you forgot 4^2, 6^2 and 6^4


#3

BRUTFORCE this may help
int Solution::hammingDistance(const vector &A) {
int i,j,x,count,ans=0;
for(i=0;i<A.size();i++){
for(j=0;j<A.size();j++){
count=0;
x=A[i]^A[j];
while(x>0){
count+=(x&1);
x>>=1;
}

        ans+=count;
    }
}
return ans;

}