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:
- 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.
- After all the queries performed, print the final list in increasing order.
Time Complexity: O(N*Q+Q*log(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:
- Initialize a variable xor with 0.
- 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.
- After traversing query add xor to the list and sort the final list.
- Print the final list after the above operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 5
#define M 2
list< int > ConstructList( int Q[N][M])
{
int xr = 0;
list< int > ans;
for ( int i = N - 1; i >= 0; i--)
{
if (Q[i][0] == 0)
ans.push_back(Q[i][1] ^ xr);
else
xr ^= Q[i][1];
}
ans.push_back(xr);
ans.sort();
return ans;
}
int main()
{
int Q[N][M] = {{ 0, 6 }, { 0, 3 },
{ 0, 2 }, { 1, 4 },
{ 1, 5 }};
list< int > ans = ConstructList(Q);
for ( auto it = ans.begin(); it != ans.end(); ++it)
cout << ' ' << *it;
}
|
Java
import java.util.*;
class GFG {
static List<Integer> ConstructList( int [][] Q)
{
int xor = 0 ;
List<Integer> ans = new ArrayList<>();
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 ];
}
ans.add(xor);
Collections.sort(ans);
return ans;
}
public static void main(String[] args)
{
int [][] Q = {
{ 0 , 6 }, { 0 , 3 }, { 0 , 2 },
{ 1 , 4 }, { 1 , 5 }
};
System.out.println(ConstructList(Q));
}
}
|
Python3
def ConstructList(Q):
xor = 0
ans = []
for i in range ( len (Q) - 1 , - 1 , - 1 ):
if (Q[i][ 0 ] = = 0 ):
ans.append(Q[i][ 1 ] ^ xor)
else :
xor ^ = Q[i][ 1 ]
ans.append(xor)
ans.sort()
return ans
Q = [ [ 0 , 6 ], [ 0 , 3 ],
[ 0 , 2 ], [ 1 , 4 ],
[ 1 , 5 ] ]
print (ConstructList(Q))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > ConstructList( int [,] Q)
{
int xor = 0;
List< int > ans = new List< int >();
for ( int i = Q.GetLength(0) - 1; i >= 0; i--)
{
if (Q[i, 0] == 0)
ans.Add(Q[i, 1] ^ xor);
else
xor ^= Q[i, 1];
}
ans.Add(xor);
ans.Sort();
return ans;
}
public static void Main(String[] args)
{
int [,] Q = {{ 0, 6 }, { 0, 3 }, { 0, 2 },
{ 1, 4 }, { 1, 5 }};
foreach ( int i in ConstructList(Q))
Console.Write(i + ", " );
}
}
|
Javascript
<script>
var N = 5
var M = 2
function ConstructList(Q)
{
var xr = 0;
var ans = [];
for ( var i = N - 1; i >= 0; i--)
{
if (Q[i][0] == 0)
ans.push(Q[i][1] ^ xr);
else
xr ^= Q[i][1];
}
ans.push(xr);
ans.sort((a,b)=>a-b);
return ans;
}
var Q = [[ 0, 6 ], [ 0, 3 ],
[ 0, 2 ], [ 1, 4 ],
[ 1, 5 ]];
var ans = ConstructList(Q);
ans.forEach(element => {
document.write( " " +element);
});
</script>
|
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Feb, 2023
Like Article
Save Article