I tried this approach in O(n). Can somebody tell me why is it failing for large cases


#1

int Solution::hammingDistance(const vector &A) {
int ans = 0;
int mod = 1000000007;
for(int i = 0; i<32; i++){
int ones = 0;
int zeroes = 0;
int ix = 1;
for(int j = 0; j<A.size(); j++){
if(A[j]&(ix<<i))
ones++;
else
zeroes++;
}
if(ones==0 || zeroes==0){
continue;
}
long long m = ((ones%mod)(zeroes%mod))%mod;
m = ((m%mod)
(2%mod))%mod;
ans =((ans%mod)+(m%mod))%mod;
}
return ans;

}


#2

return (ans) as (ans%mod)
I faced the same error. As on the last loop ans might exceed the limit.


#3

Make ones and zeros as LONG LONG INT because of the large size of the input vector. I don’t know why they don’t specify such constraints in the question, it makes debugging a lot more difficult.


#4

no its not needed…you should use long long int


#5

Sorry I am new here so I didn’t check my notifications. Thanks for the help guys.