02. Construct list using given q XOR queries
The problem can be found at the following link: Question Link
My Approach
Initialize an empty vector
ans
and add0
to it. Also, initialize a variablexorr
to0
.Iterate through each query in
queries
.If the first element of the query
x[0]
is1
, updatexorr
by XORing it withx[1]
.If the first element of the query
x[0]
is0
, appendxorr XOR x[1]
toans
.
After processing all queries, XOR each element in
ans
with the final value ofxorr
.Sort the
ans
vector.Return the sorted
ans
vector.
Time and Auxiliary Space Complexity
Time Complexity:
O(n log n)
, where n is the number of elements inans
due to the sorting step. Each query operation (XOR and append) takes constant time.Auxiliary Space Complexity:
O(n)
, where n is the number of elements in theans
vector.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated