Construct an array from XOR of all elements of array except element at same index
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 ?> |
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.
Recommended Posts:
- Construct a distinct elements array with given size, sum and element upper bound
- Construct an array from GCDs of consecutive elements in given array
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Queries for number of distinct elements from a given index till last index in an array
- Range Query on array whose each element is XOR of index value and previous element
- Construct sum-array with sum of elements in given range
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- For each element in 1st array count elements less than or equal to it in 2nd array
- Find element in array that divides all array elements
- Delete array element in given index range [L - R]
- Divide every element of one array by other array elements
- Elements of an array that are not divisible by any element of another array
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i
- Find start and ending index of an element in an unsorted array
- Find index of an extra element present in one sorted array



