We Need the Zero (Codeforces 1805A)
Tags: Others, bitmasks

There is an array $$$a$$$ consisting of non-negative integers. You can choose an integer $$$x$$$ and denote $$$b_i=a_i \oplus x$$$ for all $$$1 \le i \le n$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. Is it possible to choose such a number $$$x$$$ that the value of the expression $$$b_1 \oplus b_2 \oplus \ldots \oplus b_n$$$ equals $$$0$$$?It can be shown that if a valid number $$$x$$$ exists, then there also exists $$$x$$$ such that ($$$0 \le x < 2^8$$$).

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 10^3$$$) — the length of the array $$$a$$$.The second line of the test case contains $$$n$$$ integers — array $$$a$$$ ($$$0 \le a_i < 2^8$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$.

Output: For each set test case, print the integer $$$x$$$ ($$$0 \le x < 2^8$$$) if it exists, or $$$-1$$$ otherwise.

Note: In the first test case, after applying the operation with the number $$$6$$$ the array $$$b$$$ becomes $$$[7, 4, 3]$$$, $$$7 \oplus 4 \oplus 3 = 0$$$.There are other answers in the third test case, such as the number $$$0$$$. 

Sample Inputs:
5
3
1 2 5
3
1 2 3
4
0 1 2 3
4
1 2 2 3
1
1

Sample Outputs:
6
0
3
-1
1

Solution (Python):
t = int(input())
for _ in range(t):
    n = int(input())
    L = list(map(int, input().split()))
    A = 0
    for x in L:
        A = A ^ x
    if n % 2 == 0 and A == 0 or (n % 2 != 0 and A < 2 ** 8):
        print(A)
    else:
        print(-1)

Solution (Python):
cases = int(input())
for i in range(cases):
    tamanho = int(input())
    lista_numeros = input().split(' ')
    total_xor = 0
    for i in range(tamanho):
        total_xor ^= int(lista_numeros[i])
    if tamanho % 2 == 0 and total_xor > 0:
        print('-1')
    else:
        print(total_xor)

Solution (Python):
n = int(input())
for i in range(n):
    j = input()
    nums = [int(x) for x in input().split(' ')]
    result = 0
    for num in nums:
        result = result ^ num
    if result == 0:
        print('0')
    else:
        new_nums = [x ^ result for x in nums]
        result2 = 0
        for num in new_nums:
            result2 = result2 ^ num
        if result2 == 0:
            print(result)
        else:
            print(-1)