Construct an array from XOR of all elements of array except element at same index | C ++ program to construct array from XOR of elements of given array ; function to construct new array ; calculate xor of array ; update array ; Driver code ; print result
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructXOR ( int A [ ] , int n ) { int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) XOR ^= A [ i ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = XOR ^ A [ i ] ; } int main ( ) { int A [ ] = { 2 , 4 , 1 , 3 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; constructXOR ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << A [ i ] << " ▁ " ; return 0 ; }