Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same | C # program for the above approach ; 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
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static ArrayList rearrangeArray ( ArrayList A , ArrayList B , int N ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int xor_value = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xor_value ^= ( int ) A [ i ] ; xor_value ^= ( int ) B [ i ] ; if ( ! m . ContainsKey ( ( int ) B [ i ] ) ) m . Add ( ( int ) B [ i ] , 1 ) ; else m [ ( int ) B [ i ] ] = m [ ( int ) B [ i ] ] + 1 ; } for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = ( int ) A [ i ] ^ xor_value ; if ( m . ContainsKey ( ( int ) B [ i ] ) ) { m [ ( int ) B [ i ] ] -- ; } else return ( new ArrayList ( ) ) ; } return B ; } static void rearrangeArrayUtil ( ArrayList A , ArrayList B , int N ) { ArrayList ans = rearrangeArray ( A , B , N ) ; if ( ans . Count != 0 ) { for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } else { Console . WriteLine ( " - 1" ) ; } } public static void Main ( ) { ArrayList A = new ArrayList { 13 , 21 , 33 , 49 , 53 } ; ArrayList B = new ArrayList { 54 , 50 , 34 , 22 , 14 } ; int N = A . Count ; rearrangeArrayUtil ( A , B , N ) ; } }