A XOR value

3.2

17 votes
Algorithms, Basics of Greedy Algorithms, C++, Greedy Algorithms
Problem

You are given an array A consisting of N integer values. Find an integer K such that the value of the following function is minimized:

  • i=Ni=1(A[i] XOR K) , XOR represents a bitwise XOR operation

If multiple such K exist, then print the minimum possible value of K.

Input format

  • The first line contains an integer T denoting the number of test cases. For each test case:
  • The first line of each test case contains an integer N.
  • The second line of each test case contains N space-separated integers denoting array A.

Output format

For each test case, print the value of K in a new line.

Constraints 

1T101N1051A[i]1018

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For K = 4 ,

  • A[1] XOR 4 + A[2] XOR 4 + A[3] XOR 4 + A[4] XOR 4 = 1 XOR 4 + 0 + 0 + 0 = 5

5 is the minimum possible value of function, attained when K = 4.

Editor Image

?