Construct an array from XOR of all elements of array except element at same index
Last Updated :
13 Sep, 2023
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
Time Complexity : O(n)
Auxiliary Space : O(1)
Related Problem :
A Product Array Puzzle
Similar Reads
Construct original array starting with K from an array of XOR of all elements except elements at same index Given an array A[] consisting of N integers and first element of the array B[] as K, the task is to construct the array B[] from A[] such that for any index i, A[i] is the Bitwise XOR of all the array elements of B[] except B[i]. Examples: Input: A[] = {13, 14, 10, 6}, K = 2Output: 2 1 5 9Explanatio
6 min read
Find elements of array using XOR of consecutive elements Given an array arr[] in which XOR of every 2 consecutive elements of the original array is given i.e if the total number of elements in the original array is n then the size of this XOR array would be n-1. The first element in the original array is also given. The task is to find out the rest of n-1
8 min read
Sum of Bitwise XOR of each array element with all other array elements Given an array arr[] of length N, the task for every array element is to print the sum of its Bitwise XOR with all other array elements. Examples: Input: arr[] = {1, 2, 3}Output: 5 4 3Explanation:For arr[0]: arr[0] ^ arr[0] + arr[0] ^ arr[1] + arr[0] ^ arr[2] = 1^1 + 1^2 + 1^3 = 0 + 3 + 2 = 5For arr
9 min read
Find array whose elements are XOR of adjacent elements in given array Given an array arr[] consisting of N integers, the task is to re-construct an array arr[] such that the values in arr[] are obtained by doing XOR of the adjacent elements in the array. Print the array elements. Examples: Input: arr[ ] = {10, 11, 1, 2, 3} Output: 1 10 3 1 3 Explanation: At index 0, a
5 min read
Count arrays having at least K elements exceeding XOR of all given array elements by X given operations Given an array arr[] of size N, the task is to count the number of arrays having at least K elements greater than the XOR of all array elements, generated by performing the following operations X times. Select either first or last element from the given array.Either increment the selected element by
10 min read
Check if Sum and XOR of all elements of array is equal Given an array arr[], the task is to check if sum of all elements of an array is equal to XOR of all elements of array. Example: Input: arr[] = [1, 2] Output: YES Explanation: Sum = (1+2) = 3 XOR = (1^2) = 3 Input: arr[] = [6, 3, 7, 10] Output: NO Explanation: Sum = (6 + 3 + 7 + 10) = 26 XOR = (6 ^
4 min read