

Given an array ‘arr’ of ‘N’ integers, make a number from those set of all integers from the ‘arr’ such that if number of ‘ith’ set bits are greater than the number of ‘ith’ unset bits then make that ‘ith’ bit of the new number as set bi otherwise make that ‘ith’ bit as unset bit.
For Example:
There are three numbers, say 8, 5 and 10.
8 can be written as 1 0 0 0 .
5 can be written as 0 1 0 1.
10 can be written as 1 0 1 0.
positions of the bits as i j k l.
So we can see majority bit at ith position are set bits so ith bit will be 1. Similarly for positions of j, k and l are set as 0 0 0 respectively.
So the number generated is 1 0 0 0 i.e. 8.
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases.
Then ‘T’ test cases follows:
First line of each test case contains an integer ‘N’ representing the size of the input array ‘arr’.
Next line contains ‘N’ space separated integers denoting the elements in the ‘arr’.
Output format :
Output of each test case an integer as per the condition.
Constraints:
1 <= T <= 5
1 <= N <= 10 ^ 3
1 <= arr[i] <= 5 * (10 ^ 3)
Time Limit: 1sec
2
3
8 4 2
3
8 8 2
0
8
Test Case 1 :
Numbers can be represented in bits as:
1 0 0 0
0 1 0 0
0 0 1 0
i j k l
Number of set bits for ith index is 1. Number of unset bits for ith index is 2. Therefore the number of unset bits for ith index is greater than the number of set bits for ith index. So ith bit for the number will be 0.
Similarly for jth, kth and lth bit will be 0. As the number of unset bits is greater than the number of set bits. Answer will be 0 0 0 0 which represents number 0.
Test Case 2:
Numbers can be represented in bits as
1 0 0 0
1 0 0 0
0 0 1 0
i j k l
Number of set bits for ith index is 2. Number of unset bits for ith index is 1. Therefore the number of set bits for ith index is greater than the number of unset bits for ith index. So ith bit for the number will be 1.
Similarly for jth, kth and lth bit will be 0. As the number of unset bits is greater than the number of set bits. Answer will be 1 0 0 0 which represents the number 8.
2
5
1 2 3 4 5
4
6 7 8 9
1
0
Try thinking of counting the set bits and unset bits for every integer in the array.
The idea is to count the number of set and unset bits for every integer of ‘arr’ and for every ‘ith’ index of all positions in the maximum bit.
Approach :
O(N), where ‘N’ denotes the size of the input array.
As, we are traversing over the input array once. Therefore, overall time complexity will be O(N).
O(1)
As constant space is used. Hence, the overall space complexity is O(1).