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


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(ones==0 || zeroes==0){
long long m = ((ones%mod)(zeroes%mod))%mod;
m = ((m%mod)
ans =((ans%mod)+(m%mod))%mod;
return ans;



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


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.


no its not needed…you should use long long int


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