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

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}
 
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++
Java
Python3
C#
PHP
Javascript
// C++ program to construct array from// XOR of elements of given array#include <bits/stdc++.h>using namespace std; // function to construct new arrayvoid 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 codeint 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 program to construct array from// XOR of elements of given arrayclass 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.
# Python 3 program to construct# array from XOR of elements# of given array # function to construct new arraydef 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 codeA = [ 2, 4, 1, 3, 5 ]n = len(A)constructXOR(A, n) # print resultfor i in range(0,n):    print(A[i], end =" ") # This code is contributed by Smitha Dinesh Semwal
// C# program to construct array from// XOR of elements of given arrayusing 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// Program to construct array from// XOR of elements of given array // function to construct new arrayfunction constructXOR(&$A, $n){    // calculate xor of array    $XOR = 0;    for ($i = 0; $i < $n; $i++)        $XOR ^= $A[$i];     // update array    for ($i = 0; $i < $n; $i++)        $A[$i] = $XOR ^ $A[$i];} // Driver code$A = array( 2, 4, 1, 3, 5);$n = sizeof($A);constructXOR($A, $n); // print resultfor ($i = 0; $i < $n; $i++)    echo $A[$i] ." "; // This code is contributed// by ChitraNayal?>
<script> // JavaScript program to construct array from// XOR of elements of given array // function to construct new arrayfunction constructXOR(A, n){    // calculate xor of array    let XOR = 0;    for (let i = 0; i < n; i++)        XOR ^= A[i];     // update array    for (let i = 0; i < n; i++)        A[i] = XOR ^ A[i];} // Driver code    let A = [ 2, 4, 1, 3, 5];    let n = A.length;    constructXOR(A, n);     // print result    for (let i = 0; i < n; i++)        document.write(A[i] + " "); // This code is contributed by Surbhi Tyagi. </script>
Output: 
3 5 0 2 4
Related Problem : A Product Array PuzzleThis article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
ukasp
surbhityagi15
Bitwise-XOR
Arrays
Bit Magic
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Introduction to Arrays
Linear Search
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
Cyclic Redundancy Check and Modulo-2 Division