Xor Equation ( DSA INTERVIEW PREPARATION PROBLEMS AND SOLUTIONS )

 You are given associate degree array A of N non-negative integers, wherever N is odd. notice the minimum non-negative number x that satisfies the equation


(A1+x)⊕(A2+x)⊕⋯⊕(AN+x)=0
where wherever denotes the bitwise XOR operation. If no such x exists, print −1.

Note: The input of this drawback is massivetherefore use quick input/output strategies.

Input Format
First line of the input contains one number T, the amount of check cases. the outline of T check cases follows.
The first line of every legal action contains one number N, denoting the scale of the array A.
The second line of every legal action contains N space-separated non-negative integers A1,A2…AN.
Output Format
For each legal action, print one line containing one number. If there doesn't exist any non-negative number x that satisfies the given equation, print −1. Otherwise, print the minimum worth of x that satisfies the given equation.

Constraints
1≤T≤105
1≤N≤106
0≤Ai≤1018
The value of N is absolute to be odd.
Sum of N over all check cases is a smaller amount than or capable 106.
Subtasks
Subtask one (20 points):

1≤T≤103
1≤N≤103
0≤Ai≤103
The value of N is absolute to be odd.
Sum of N over all check cases is a smaller amount than or capable 103.
Subtask two (80 points):

Original constraints
Sample Input one
4
3
2 4 5
3
0 0 0
3
1 1 1
3
1 1 2
Sample Output one
1
0
-1
-1
Explanation
Test case 1:

We have to seek out minimum non-negative number x specified
(2+x)⊕(4+x)⊕(5+x)=0
Let f(x)=(2+x)⊕(4+x)⊕(5+x).

For x=0, we have
f(0)=(2+0)⊕(4+0)⊕(5+0)=2⊕4⊕5=3
For x=1, we have
f(1)=(2+1)⊕(4+1)⊕(5+1)=3⊕5⊕6=0
Therefore, x=1 is that the minimum non-negative number that satisfies the given equation.

Test case 2:

We have to seek out minimum non-negative number x specified
(0+x)⊕(0+x)⊕(0+x)=0
i.e. x⊕x⊕x=0. however since 0⊕0⊕0=0, it follows that x=0 is that the minimum non-negative number that satisfies the given equation.

Test case 3:

We have to seek out minimum non-negative number x specified
(1+x)⊕(1+x)⊕(1+x)=0
But however and 0⊕a=a for any non-negative number a. It follows that
(1+x)⊕(1+x)⊕(1+x)=0⊕(1+x)=1+x
This implies that x satisfies the given equation if and as long as it satisfies 1+x=0 if and as long as x=−1. however however may be a negative number and so, there doesn't exist any x that satisfies the given equation.

Test case 4:

We have to seek out minimum non-negative number x specified
(1+x)⊕(1+x)⊕(2+x)=0
It follows that
(1+x)⊕(1+x)⊕(2+x)=0⊕(2+x)=2+x
This implies that x satisfies the given equation if and as long as it satisfies 2+x=0, which may solely happen if x=−2. however however may be a negative number and so, there doesn't exist any non-negative x that satisfies the given equation, therefore we tend to output −1.

Code: click
Previous
Next Post »