Can someone please explain to me the following parts in the Java editorial solution?


#1

1. What is the meaning of the multiplication and sum we do in the 2nd cycle?
Why are we multiplying the number of 1s obtained for a given bit-position by the number of 0s obtained for a given bit-position, in all the set of N numbers?

2. Why do we apply the module twice?

public int cntBits(List<Integer> a) {
    int[] count1=new int[32];
    for (int n:a) {
        for (int i=0; i<32; i++) {
            count1[i]+=n & 1;
            n=n>>1;
        }
    }
    long res=0;
    for (int i=0; i<32; i++) {
        res+=((long)count1[i]*(a.size()-count1[i]))%1000000007;
    }
    return (int)(2*res%1000000007);
}

Thanks in advance


#2

If you want to know how the value of the result is getting count1[i](size - count1[i])
Think of a string 00000…0111…1 where 0 is m times and 1 is n times .Now the total length is m+n. Think of it as how many ways we can create pair (i,j) such that i,j is in {0,1} and i is not equal to j
Obviously there will be m
n pairs .(for every 0 get a unique 1) . since the number of 0s = (len - n) The other way would have been (for every 1 get a unique 0) again mn So total ways = 2mn = 2*(len - n)*n Since we have only precomputed for n we could have done the same for 0s as well.Modulus is used twice in the explaination just to avoid overflow in the calculation