Skip to content
Related Articles

Related Articles

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}

 

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 <bits/stdc++.h>
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




<?php
// Program to construct array from
// XOR of elements of given array
 
// function to construct new array
function 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 result
for ($i = 0; $i < $n; $i++)
    echo $A[$i] ." ";
 
// This code is contributed
// by ChitraNayal
?>

Javascript




<script>
 
// JavaScript program to construct array from
// XOR of elements of given array
 
// function to construct new array
function 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 Puzzle
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :