Construct a List using the given Q XOR queries | C ++ program for the above approach ; Function to return required list after performing all the queries ; Store cumulative Bitwise XOR ; Initialize final list to return ; Perform each query ; The initial value of 0 ; Sort the list ; Return final list ; Driver Code ; Given Queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  5 NEW_LINE #define M  2 NEW_LINE 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 ; }