Why is it getting wrong output for [96,96,7,81,2,13]


#1

string convertbinary(int n){
string binary = “”;
while(n != 0){
binary += to_string(n % 2);
n = n/2;
}
reverse(binary.begin(), binary.end());
return binary;

}

vector pairmaker(vector A){

vector<string> binary;
for(int i = 0; i < A.size(); i++){
    string temp = convertbinary(A[i]);
    binary.push_back(temp);
}


return binary;

}
vector size(vector A){
vector sizevec;
for(int i = 0; i < A.size(); i++){
sizevec.push_back(A[i].size());
}
return sizevec;
}
int Solution::hammingDistance(const vector &A) {

if(A.size() == 1){
    return 0;
}
vector<string> binary;
vector<int> sizevec;
binary = pairmaker(A);
sizevec = size(binary);
int count = 0;
for(int i = 0; i < binary.size(); i++){
    for(int j = i + 1; j < binary.size(); j++){
        if(abs(sizevec[i] - sizevec[j]) > 0){
            count += abs(sizevec[i] - sizevec[j]);
        }
        for(int k = 0; k < min(sizevec[i], sizevec[j]); k++){
            if(binary[i][k] != binary[j][k] ){
                count += 1;
            }
        }
    }
}
return 2*count;

}


#2

For me, it was giving problem because I was running the loop for the 32th bit from right which is not right because it is the sign bit and it should not be included. I only ran the loop from 0 to 31 and it worked.