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 ; Driver Code ; Given vector A ; Given vector B ; Size of the vector ; Function Call
def rearrangeArray ( A , B , N ) : NEW_LINE INDENT m = { } NEW_LINE xor_value = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT xor_value ^= A [ i ] NEW_LINE xor_value ^= B [ i ] NEW_LINE if B [ i ] in m : NEW_LINE m [ B [ i ] ] = m [ B [ i ] ] + 1 NEW_LINE else : NEW_LINE m [ B [ i ] ] = 1 NEW_LINE DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT B [ i ] = A [ i ] ^ xor_value NEW_LINE if B [ i ] in m : NEW_LINE m [ B [ i ] ] = m [ B [ i ] ] - 1 NEW_LINE else : NEW_LINE X = [ ] NEW_LINE return X NEW_LINE DEDENT return B NEW_LINE DEDENT def rearrangeArrayUtil ( A , B , N ) : NEW_LINE INDENT ans = rearrangeArray ( A , B , N ) NEW_LINE if ( len ( ans ) > 0 ) : NEW_LINE INDENT for x in ans : NEW_LINE INDENT print ( x , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 13 , 21 , 33 , 49 , 53 ] NEW_LINE B = [ 54 , 50 , 34 , 22 , 14 ] NEW_LINE N = len ( A ) NEW_LINE rearrangeArrayUtil ( A , B , N ) NEW_LINE DEDENT