# XOR Query

Posted: 27 Nov, 2020
Difficulty: Easy

## PROBLEM STATEMENT

#### The queries are of two types:

``````1. 1 ‘VAL’, for this type of query, you need to insert the integer 'VAL' to the end of the array.
2. 2 ‘VAL’, for this type of query, you need to take the bitwise XOR of all the elements of the array with 'VAL' i.e each element of the array ‘ARR’ will be updated as ‘ARR[i]’ = ‘ARR[i]’ ^ ‘VAL’ ( ^ denotes the bitwise XOR operation).
``````

#### Note:

``````1) Bitwise XOR operation takes two numbers and performs XOR operation on every bit of those two numbers. For example, consider two numbers 2 and 3 their bitwise XOR will be 1. Because the binary representation of 2 is '10' and the binary representation of 3 is '11'. And XOR of '10' and '11' will be '01'(because XOR evaluates to 0 if the corresponding bits are the same in both the operands, otherwise it evaluates to 1), which is equal to 1.

2) The first query will always be a type 1 query.

3) Note that the ith query should be performed on the array obtained after performing (i-1)th query on the array and so on i.e the changes of each query are updated on the original array itself.
``````
##### Input Format:
``````The first line contains an integer ‘T’ which represents the number of test cases.

The first line of each test case contains an integer ‘Q’ denoting the number of queries.

Then each of the ‘Q’ lines contains two space-separated integers denoting the query to be performed.
``````
##### Output Format:
``````For each test case, return the updated array after processing all the queries.
``````
##### Note:
``````You do not need to print anything, it has already been taken care of. Just implement the given function.
``````
##### Constraints:
``````1 <= T <= 10
1 <= Q <= 10^5
1 <= Val <= 10^9

Time Limit: 1sec
``````
``````Can you solve this in constant i.e O(1) space complexity? Space used to return the list will not be counted as an extra space.
`````` Approach 1

#### Approach:

• It is a brute force approach where we will create an empty array initially of size 10^5 + 1.
• Now, whenever we have a query of type 1 we will insert the value of VAL in the array. And when the query of type 2 comes then we will iterate through our array and perform an XOR operation with VAL to every value present in our array.
• Finally, we will return our array.

#### Algorithm:

• Create an array ans.
• Now iterate through all the queries and for each query perform the following operation:
• If the query is of type 1 then insert the VAL in the back of the array.
• If the query is of type 2 then:
• Iterate through the array ans and perform XOR operation with the value of VAL to every element present in the array i.e ans[i] = ans[i] ^ VAL.
• Finally, return the array ans.