Construct a List using the given Q XOR queries | Javascript 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 ; Given Queries ; Function Call
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 ) ; } ) ;