# Construct an array from XOR of all elements of array except element at same index

• Difficulty Level : Easy
• Last Updated : 08 Mar, 2021

Given an array A[] having n positive elements. The task to create another array B[] such as B[i] is XOR of all elements of array A[] except A[i].
Examples :

```Input : A[] = {2, 1, 5, 9}
Output : B[] = {13, 14, 10, 6}

Input : A[] = {2, 1, 3, 6}
Output : B[] = {4, 7, 5, 0}```

Recommended Practice

Naive Approach :
We can simple calculate B[i] as XOR of all elements of A[] except A[i], as

```for (int i = 0; i < n; i++)
{
B[i] = 0;
for (int j = 0; j < n; j++)
if ( i != j)
B[i] ^= A[j];
}```

Time complexity for this naive approach is O (n^2).
Auxiliary Space for this naive approach is O (n).
Optimized Approach :
First calculate XOR of all elements of array A[] say ‘xor’, and for each element of array A[] calculate A[i] = xor ^ A[i]

```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];```

Time complexity for this approach is O (n).
Auxiliary Space for this approach is O (1).

## C++

 `// C++ program to construct array from``// XOR of elements of given array``#include ``using` `namespace` `std;` `// function to construct new array``void` `constructXOR(``int` `A[], ``int` `n)``{``    ``// calculate xor of array``    ``int` `XOR = 0;``    ``for` `(``int` `i = 0; i < n; i++)``        ``XOR ^= A[i];` `    ``// update array``    ``for` `(``int` `i = 0; i < n; i++)``        ``A[i] = XOR ^ A[i];``}` `// Driver code``int` `main()``{``    ``int` `A[] = { 2, 4, 1, 3, 5};``    ``int` `n = ``sizeof``(A) / ``sizeof``(A[0]);``    ``constructXOR(A, n);` `    ``// print result``    ``for` `(``int` `i = 0; i < n; i++)``        ``cout << A[i] << ``" "``;``    ``return` `0;``}`

## Java

 `// Java program to construct array from``// XOR of elements of given array``class` `GFG``{``    ` `    ``// function to construct new array``    ``static` `void` `constructXOR(``int` `A[], ``int` `n)``    ``{``        ` `        ``// calculate xor of array``        ``int` `XOR = ``0``;``        ``for` `(``int` `i = ``0``; i < n; i++)``            ``XOR ^= A[i];``    ` `        ``// update array``        ``for` `(``int` `i = ``0``; i < n; i++)``            ``A[i] = XOR ^ A[i];``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `A[] = { ``2``, ``4``, ``1``, ``3``, ``5``};``        ``int` `n = A.length;``        ``constructXOR(A, n);``    ` `        ``// print result``        ``for` `(``int` `i = ``0``; i < n; i++)``            ``System.out.print(A[i] + ``" "``);``    ``}``}` `// This code is contributed by Anant Agarwal.`

## Python3

 `# Python 3 program to construct``# array from XOR of elements``# of given array` `# function to construct new array``def` `constructXOR(A, n):``    ` `    ``# calculate xor of array``    ``XOR ``=` `0``    ``for` `i ``in` `range``(``0``, n):``        ``XOR ^``=` `A[i]` `    ``# update array``    ``for` `i ``in` `range``(``0``, n):``        ``A[i] ``=` `XOR ^ A[i]` `# Driver code``A ``=` `[ ``2``, ``4``, ``1``, ``3``, ``5` `]``n ``=` `len``(A)``constructXOR(A, n)` `# print result``for` `i ``in` `range``(``0``,n):``    ``print``(A[i], end ``=``" "``)` `# This code is contributed by Smitha Dinesh Semwal`

## C#

 `// C# program to construct array from``// XOR of elements of given array``using` `System;` `class` `GFG``{``    ` `    ``// function to construct new array``    ``static` `void` `constructXOR(``int` `[]A, ``int` `n)``    ``{``        ` `        ``// calculate xor of array``        ``int` `XOR = 0;``        ``for` `(``int` `i = 0; i < n; i++)``            ``XOR ^= A[i];``    ` `        ``// update array``        ``for` `(``int` `i = 0; i < n; i++)``            ``A[i] = XOR ^ A[i];``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``int` `[]A = { 2, 4, 1, 3, 5};``        ``int` `n = A.Length;``        ``constructXOR(A, n);``    ` `        ``// print result``        ``for` `(``int` `i = 0; i < n; i++)``        ``Console.Write(A[i] + ``" "``);``    ``}``}` `// This code is contributed by nitin mittal`

## PHP

 ``

## Javascript

 ``

Output:

`3 5 0 2 4`

Related Problem :
A Product Array Puzzle