Timer is Running

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

2

1 2

3

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

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.

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.

abnandi04 | 801 |

KshamaGupta | 412 |

srivastavad270 | 223 |

bhavyapratap56 | 206 |

anshusingh20112001 | 181 |

abnandi04 | 776 |

KshamaGupta | 296 |

srivastavad270 | 223 |

bhavyapratap56 | 206 |

afreensartaj131 | 170 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4748 |

Login to report an issue on this page.