 Timer is Running

Sum of bit differences
##### Submissions: 13256   Accuracy: 52.16%   Difficulty: Medium   Marks: 4

Write a program to find the sum of bit differences in all pairs that can be formed from array elements n. Bit difference of a pair (x, y) is a count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 ( first and last bits differ in two numbers).

Input: The first line of input contains an integer T denoting the number of test cases. First line of the test case will contain an array of elements n.

Output: The sum of bit differences of all pairs that can be formed by given array.

Constraints:

1 <=T<= 100

1 <=N<= 10

1 <=a[i]<= 10

Example:

Input:
2

1 2

1 3 5

Output:
4
8

Note: (a, b) and (b, a) are considered two separate pairs.

Explanation:
Test Case 1:

1: 01
2: 10
Bit difference in pair (1, 2): 2
Bit difference in pair (2, 1): 2
Hence, total Bit difference = 2 + 2 = 4

Test Case 2:
1: 001
3: 011
5: 101
Bit difference in pair (1, 3): 1
Bit difference in pair (3, 1): 1
Bit difference in pair (1, 5): 1
Bit difference in pair (5, 1): 1
Bit difference in pair (3, 5): 2
Bit difference in pair (5, 3): 2
Hence, total Bit difference = 1+1+1+1+2+2 = 8.

#### ** For More Input/Output Examples Use 'Expected Output' option **

Author: atharv

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.