Construct a List using the given Q XOR queries | Java 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
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 ) ) ; } }