New update is available. Click here to update.

Last Updated: 7 Jan, 2021

Difficulty: Easy

```
1. If ('ARRi', 'ARRj') is a pair, then ('ARRj', 'ARRi') will also be considered as a pair.
2. Both positive and negative numbers may be present.
3. Don't ignore the negative sign of the number.
```

```
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next 2* T lines represent the ‘T’ test cases.
The first line of each test case contains an integer ‘N’ denoting the given integer array size.
The second line of each test case contains the ‘N’ space-separated integers denoting the array elements.
```

```
For each test case, return the sum of bit differences among all the pairs.
```

```
You do not need to print anything. It has already been taken care of. Just implement the given function.
```

```
1 <= T <= 50
1 <= N <= 300
-10^9 <= ARR[i] <= 10^9
Where ARR[i] is the 'i-th' element of the given array.
Time Limit: 1 sec
```

- Traverse a for loop from 0 to 31. In each iteration of the for loop focus on a particular bit position.
- In the i-th iteration, calculate the integers in the given array with the i-th bit set and store it in a variable ‘COUNTSETBITS’.
- The number of integers in the given array with the i-th bit not set is equal to ‘N' - 'COUNTSETBITS’.
- The count of bits difference among all pairs at an i-th bit position will be ('N' * ('N' - 'COUNTSETBITS')) * 2. We multiply by 2 because the difference of set bits in pair('ARRi', ‘ARRj’) is equal to the difference of set bits in ('ARRj', ‘ARRi’).

SIMILAR PROBLEMS

Longest Subarray With Zero Sum

Posted: 3 Nov, 2022

Difficulty: Moderate

Merge Two Sorted Arrays Without Extra Space

Posted: 19 Nov, 2022

Difficulty: Moderate

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Co-Prime

Posted: 14 Dec, 2022

Difficulty: Hard

Negative To The End

Posted: 16 Dec, 2022

Difficulty: Easy

Popular Interview Problems: