388535 (Easy Version) (Codeforces 1658D1)
Tags: Math, bitmasks

This is the easy version of the problem. The difference in the constraints between both versions is colored below in red. You can make hacks only if all versions of the problem are solved.Marin and Gojou are playing hide-and-seek with an array.Gojou initially performs the following steps:   First, Gojou chooses $$$2$$$ integers $$$l$$$ and $$$r$$$ such that $$$l \leq r$$$.  Then, Gojou makes an array $$$a$$$ of length $$$r-l+1$$$ which is a permutation of the array $$$[l,l+1,\ldots,r]$$$.  Finally, Gojou chooses a secret integer $$$x$$$ and sets $$$a_i$$$ to $$$a_i \oplus x$$$ for all $$$i$$$ (where $$$\oplus$$$ denotes the bitwise XOR operation). Marin is then given the values of $$$l,r$$$ and the final array $$$a$$$. She needs to find the secret integer $$$x$$$ to win. Can you help her?Note that there may be multiple possible $$$x$$$ that Gojou could have chosen. Marin can find any possible $$$x$$$ that could have resulted in the final value of $$$a$$$.

Input: The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases.In the first line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$ \color{red}{\boldsymbol{0} \boldsymbol{=} \boldsymbol{l}} \le r < 2^{17}$$$).The second line contains $$$r - l + 1$$$ integers of $$$a_1,a_2,\ldots,a_{r-l+1}$$$ ($$$0 \le a_i < 2^{17}$$$). It is guaranteed that $$$a$$$ can be generated using the steps performed by Gojou.It is guaranteed that the sum of $$$r - l + 1$$$ over all test cases does not exceed $$$2^{17}$$$.

Output: For each test case print an integer $$$x$$$. If there are multiple answers, print any.

Note: In the first test case, the original array is $$$[3, 2, 1, 0]$$$. In the second test case, the original array is $$$[0, 3, 2, 1]$$$.In the third test case, the original array is $$$[2, 1, 0]$$$.

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

Sample Outputs:
0
4
3

Solution (Python):
import sys
input = sys.stdin.buffer.readline

def solve(a, l, r):
    for i in a:
        if i ^ 1 not in a:
            for i_org in [l, r]:
                x = i ^ i_org
                if a == {j_org ^ x for j_org in range(l, r + 1)}:
                    return x
    return solve({i >> 1 for i in a}, l >> 1, r >> 1) << 1
for _ in range(int(input())):
    (l, r) = map(int, input().split())
    a = list(map(int, input().split()))
    print(solve(set(a), l, r))

Solution (Python):
t = int(input())
for test in range(t):
    (l, r) = map(int, input().split())
    a = list(map(int, input().split()))
    for j in range(17):
        if (r - l + 1) % pow(2, j + 1) != 0:
            break
    b = a
    visit = set()
    if j > 0:
        for i in range(r - l + 1):
            visit.add(a[i] >> j)
        b = list(visit)
    xor = 0
    for x in range(l, r + 1, pow(2, j)):
        xor ^= x >> j
    for x in b:
        xor ^= x
    print(xor * pow(2, j))

Solution (Python):
import sys
input = sys.stdin.buffer.readline
t = int(input())
res = []
for _ in range(t):
    (l, r) = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    n = len(a)
    ones = [0] * 17
    zeroes = [0] * 17
    for v in a:
        for i in range(17):
            if v & 1 << i > 0:
                ones[i] += 1
            else:
                zeroes[i] += 1
    x = 0
    for i in range(17):
        if ones[i] > zeroes[i]:
            x = x ^ 1 << i
    res.append(x)
print('\n'.join([str(x) for x in res]))