# Construct a List using the given Q XOR queries

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Given a list S that initially contains a single value 0. Below are the Q queries of the following types:

• 0 X: Insert X in the list
• 1 X: For every element A in S, replace it by A XOR X.

The task is to print all the element in the list in increasing order after performing the given Q queries.

Examples:

Input:_ Q[][] = { {0, 6}, {0, 3}, {0, 2}, {1, 4}, {1, 5} }_

Output:_ 1 2 3 7_

Explanation:

[0] (initial value)

[0 6] (add 6 to list)

[0 6 3] (add 3 to list)

[0 6 3 2] (add 2 to list)

[4 2 7 6] (XOR each element by 4)

[1 7 2 3] (XOR each element by 5)

Thus sorted order after performing queries is [1 2 3 7]

Input:_ Q[][]= {{0, 2}, {1, 3}, {0, 5} }_

Output:_ 1 3 5_

Explanation:

[0] (initial value)

[0 2] (add 2 to list)

[3 1] (XOR each element by 3)

[3 1 5] (add 5 to list)

Thus sorted order after performing queries is [1 3 5]

Naive Approach: The simplest approach to solve the problem is:

1. Initialize a list with 0 then traverse for each query and do the following:
• For query type 0 add given value in the list.
• For query type 1 traverse list and update every element with their respective Bitwise XOR with value.
1. After all the queries performed, print the final list in increasing order.

Time Complexity:_ O(N*Q), where N is the length of the list and Q is the number of queries_

Auxiliary Space:_ O(1)_

Efficient Approach: It is much easier to process the numbers in reverse order by keeping track of the cumulative Bitwise XOR working from right to left. Follow the steps below to solve the problem:

1. Initialize a variable xor with** 0.**
2. Iterate over the query array from right to left, add xor^value to the list while query type is 0 otherwise update xor with xor^value.
3. After traversing query add xor to the list and sort the final list.
4. Print the final list after the above operations.

Below is the implementation of the above approach:

• Java
• Python3
• C#

`// Java program for the above approach`

`**import**` `java.util.*;`

`**class**` `GFG {`

`// Function to return required list`

`// after performing all the queries`

`**static**` `List<Integer> ConstructList(``**int**``[][] Q)`

`{`

`// Store cumulative Bitwise XOR`

`**int**` `xor =` `0``;`

`// Initialize final list to return`

`List<Integer> ans =` `**new**` `ArrayList<>();`

`// Perform each query`

`**for**` `(``**int**` `i = Q.length -` `1``; i >=` `0``; i--) {`

`**if**` `(Q[i][``0``] ==` `0``)`

`ans.add(Q[i][``1``] ^ xor);`

`**else**`

`xor ^= Q[i][``1``];`

`}`

`// The initial value of 0`

`ans.add(xor);`

`// Sort the list`

`Collections.sort(ans);`

`// Return final list`

`**return**` `ans;`

`}`

`// Driver Code`

`**public**` `**static**` `**void**` `main(String[] args)`

`{`

`// Given Queries`

`**int**``[][] Q = {`

`{` `0``,` `6` `}, {` `0``,` `3` `}, {` `0``,` `2` `},`

`{` `1``,` `4` `}, {` `1``,` `5` `}`

`};`

`// Function Call`

`System.out.println(ConstructList(Q));`

`}`

`}`

Output:

``````[1, 2, 3, 7]
``````

Time Complexity:_ O(Q * log(Q))_, where Q is the number of queries.

Auxiliary Space:_ O(N)_, where N is the number of elements in the resultant list.

Attention reader! Donâ€™t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

## Queries to find the Lower Bound of K from Prefix Sum Array

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Queries to find the XOR of an Array after replacing all occurrences of X by Y

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Count pairs having Bitwise XOR less than K from given array

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Bitwise operations on Subarrays of size K

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Sum of prime numbers in range [L, R] from given Array for Q queries

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.