The first line contains a single integer ‘T’ denoting the number of test cases to run. Then the test cases follow.
The first line contains a single integer N, where N represents the size of the array.
The next line contains N integers, denoting the elements of the given array.
For each test case print a single integer denoting the sum of special numbers in the array.
Output for each test case will be printed in a separate line,
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 2 * 10^4
1 <= a[i] <= 10^9
Time limit: 1 sec
2
6
1 2 3 4 5 6
4
4 8 16 32
2
0
For test case 1:
The binary representation of 2 is (10)2 it has an equal number of bits with values 1 and value 0.
Rest all the numbers have unequal count of bits with values 0 and 1.
Hence the final sum is 0+2+0+0+0+0 = 2
For test case 2:
None of the array elements has an equal count of bits with values 0 and 1.
4=(100) , 8=(1000) , 16=(10000) ,32=(100000)
2
2
12 9
1
12
21
12
For each number of the array, we directly calculate the count of bits with value = 1 in its binary representation using __builtin_popcount.
To calculate the count of bits with value = 0, we need to find the position of the leftmost bit with value 1 and deduct the count of 1’s from its position.
For example: 49 = (0000000110001)2
__builtin_popcount(49) = 3,
Position of MSB = log2(49) = 5, as indexing is 0 based, so there are total 6 bits
Therefore count of 0’s = 6-3 = 3
The steps are as follows: