Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same | Function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store frequency of elements ; Stores xor value ; Taking xor of all the values of both arrays ; Store frequency of B [ ] ; Find the array B [ ] after rearrangement ; If the updated value is present then decrement its frequency ; Otherwise return empty vector ; Utility function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store rearranged array B ; If rearrangement possible ; Otherwise return - 1 ; Given vector A ; Given vector B ; Size of the vector ; Function Call
function rearrangeArray ( A , B , N ) { let m = new Map ( ) ; let xor_value = 0 ; for ( let i = 0 ; i < N ; i ++ ) { xor_value ^= A [ i ] ; xor_value ^= B [ i ] ; if ( m . has ( B [ i ] ) ) { m . set ( B [ i ] , m . get ( B [ i ] ) + 1 ) } else { m . set ( B [ i ] , 1 ) } } for ( let i = 0 ; i < N ; i ++ ) { B [ i ] = A [ i ] ^ xor_value ; if ( m . has ( B [ i ] ) ) { m . set ( B [ i ] , m . get ( B [ i ] ) - 1 ) } else return [ ] ; } return B ; } function rearrangeArrayUtil ( A , B , N ) { let ans = rearrangeArray ( A , B , N ) ; if ( ans . length > 0 ) { for ( let x of ans ) { document . write ( x + " " ) ; } } else { document . write ( " " ) ; } } let A = [ 13 , 21 , 33 , 49 , 53 ] ; let B = [ 54 , 50 , 34 , 22 , 14 ] ; let N = A . length ; rearrangeArrayUtil ( A , B , N ) ;