Given a set S with the initial element 0 that is S = { 0 }. The task is to perform each query when Q number of queries are given and print the answer after every query of type 3.
We can perform three types of query operations:
 1 X: We can add X to the set S.
 2 Y: For each element i, perform i ⊕ Y.
 3: Find the minimum element of the set.
Examples:
Input: Q = 10,
3
1 7
3
2 4
2 8
2 3
1 10
1 3
3
2 1
Output: 0 0 3Explanation:
For the given 10 queries, the changes in the set for each query is as follows:
 The minimum is 0.
 The number 7 added to S –> {0, 7}.
 The minimum is still 0.
 All of the numbers in S are changed to their xor with 4 –> {4, 3}.
 All of the numbers in S are changed to their xor with 8 –> {12, 11}.
 All of the numbers in S are changed to their xor with 3 –> {15, 8}.
 The number 10 added to S –> {15, 8 ,10}.
 The number 3 added to S –> {15, 8, 10, 3}.
 The minimum is now 3.
 All of the numbers in S are changed to their xor with 1 –> {14, 9, 11, 2}.
Input: Q = 6
3
1 7
3
1 4
2 8
3
Output: 0 0 8
Prerequisite: Trie.
Approach:
We will try to solve this problem by using the trie approach of Minimum XOR Value Pair Problem.
 So, In this problem, we have a binary trie and an integer x, we have to find the minimum value of XOR(x, y) where y is some integer from the trie.
 Now, To solve this problem we will go down the trie from the most significant bit to the least.

Suppose we are at ith bit:
If x[i] is 1, we will go down the path of the trie which has 1.
If x[i] is 0, we will go down the path which has 0.
If at position i, we do not have a branch to go down x[i], we will go the other way.
Now, coming to our problem.
 Suppose we have inserted a1, a2, a3 in the set and then xor everything with x1, x2, x3, then it is same as XORing with X = XOR(x1, x2, x3).

So, finding the minimum element is equivalent to finding the minimum among (a1, a2, a3) after XORing with X.
We have already noticed how to do that in the beginning. 
Now, How to answer each of the queries.
Let x = XOR(x1, x2, ….., xn), where x1, x2, …, xn are all numbers asked to XOR with the element of the set S.
For Query 2, XOR with xi.
we will not XOR every element in the trie with xi. instead, we will just update x = XOR(x, xi) 
For Query 3, getting the minimum element.
We were supposed to have XORed the entire array so far with X.
So, now we will just calculate the minimum value obtained by taking XOR of all the elements in the set with X by using the abovementioned approach. 
For Query 1, insert ai.
We will not insert ai into the trie, but XOR(ai, x). Reason: Suppose we Insert a1, a2, then XOR with x1, then insert a3, then XOR with x2.

When we query for minimum now, we will find the minimum value in : {XOR(a1, x1, x2), XOR(a2, x1, x2), XOR(a3, x1, x2)}
But a3 has only been XORed with x2 and not x1. 
So it is critical that at every moment in time that we insert an element ai into the trie, we insert XOR(ai, x). This ensures that when we calculate the minimum, it will cancel out the previous XORs.
So, in our example, our trie will contain
{a1, a2, XOR(a3, x1)}.  When we query the minimum value of XOR(x), we will be finding the minimum using above method of {XOR(a1, x1, x2), XOR(a2, x1, x2), XOR(a3, x2)}, which is what we want. Inserting XOR(ai, x) will ensure that whatever we do the minimum operation, we do not do any unnecessary XORs on any ai.

For Query 2, XOR with xi.
Below is the C++ implementation of this approach:
// C++ program to operate // queries in a given set #include <bits/stdc++.h> using namespace std; const int Maxbits = 30; // Function for Query 1 void Insert( int x, int curx, int * sz, int trie[100][2]) { // XOR each element before // storing it in the trie. x = x ^ curx; int p = 0; // Storing xored element in the trie. for ( int i = Maxbits  1; i >= 0; i) { if (!trie[p][x >> i & 1]) trie[p][x >> i & 1] = (*sz)++; p = trie[p][x >> i & 1]; } } // Function for Query 2 void XorQuery( int x, int * curx) { // Simply xoring all the number which // was asked to xor with the set elements. (*curx) = (*curx) ^ x; } // Function for Query 3 void MinXor( int x, int trie[100][2]) { int ans = 0, p = 0; // Finding the minimum element by checking // if x[i] bit is same with trie element. for ( int i = Maxbits  1; i >= 0; i) { bool Currbit = (x >> i & 1); if (trie[p][Currbit]) p = trie[p][Currbit]; else { p = trie[p][!Currbit]; ans = 1 << i; } } cout << ans << endl; } // Driver code int main() { int sz = 1; int curx = 0; int trie[100][2] = { 0 }; // Initialising the trie Insert(0, 0, &sz, trie); // Calling the Query MinXor(curx, trie); Insert(7, curx, &sz, trie); MinXor(curx, trie); XorQuery(4, &curx); XorQuery(8, &curx); XorQuery(3, &curx); Insert(10, curx, &sz, trie); Insert(3, curx, &sz, trie); MinXor(curx, trie); XorQuery(1, &curx); return 0; } 
0 0 3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a studentfriendly price and become industry ready.
Recommended Posts:
 Range Queries to Find number of subarrays with a given xor
 Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
 Range Update Queries to XOR with 1 in a Binary Array.
 Queries to update Subarrays of a given Array using Disjoint Set
 Queries for Count of divisors of product of an Array in given range  Set 2 (MO's Algorithm)
 Find the element having maximum set bits in the given range for Q queries
 Queries to multiply the given subarray with given number X and print sum
 Segment Tree  Set 3 (XOR of given range)
 Maximum XOR with given value in the path from root to given node in the tree
 Efficiently design Insert, Delete and Median queries on a set
 Index of kth set bit in a binary array with update queries
 Queries to find kth smallest element and point update : Ordered Set in C++
 Proto Van Emde Boas Tree  Set 5  Queries: Minimum, Maximum
 Van Emde Boas Tree  Set 2  Insertion, Find, Minimum and Maximum Queries
 Queries for number of distinct elements in a subarray  Set 2
 Queries to find the leftmost given type integer in a binary array
 Queries to update a given index and find gcd in range
 String Range Queries to find the number of subsets equal to a given String
 Queries for number of distinct elements from a given index till last index in an array
 Perform append, update, delete and range sum queries on the given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.