Open In App

Construct original array starting with K from an array of XOR of all elements except elements at same index

Last Updated : 11 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 = 2
Output: 2 1 5 9
Explanation:
For any index i, A[i] is the Bitwise XOR of all elements of B[] except B[i]. 

  1. B[1] ^ B[2] ^ B[3] = 1 ^ 5 ^ 9 = 13 = A[0]
  2. B[0] ^ B[2] ^ B[3] = 2 ^ 5 ^ 9 = 14 = A[1]
  3. B[0] ^ B[1] ^ B[3] = 2 ^ 1 ^ 9 = 10 = A[2]
  4. B[0] ^ B[1] ^ B[2] = 2 ^ 1 ^ 5 = 6 = A[3]

Input: A[] = {3, 5, 0, 2, 4}, K = 2
Output: 2 4 1 3 5

 

Approach: The idea is based on the observation that Bitwise XOR of the same value calculated even number of times is 0.

For any index i, 
A[i] = B[0] ^ B[1] ^ … B[i-1] ^ B[i+1] ^ … B[n-1] 
Therefore, XOR of all elements of B[], totalXor = B[0] ^ B[1] ^ … B[i – 1] ^ B[i] ^ B[i + 1] ^ … ^ B[N – 1].
Therefore, B[i] = totalXor ^ A[i]. (Since every element occurs twice except B[i])

Follow the below steps to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to construct an array
// with each element equal to XOR
// of all array elements except
// the element at the same index
void constructArray(int A[], int N,
                    int K)
{
    // Original array
    int B[N];
 
    // Stores Bitwise XOR of array
    int totalXOR = A[0] ^ K;
 
    // Calculate XOR of all array elements
    for (int i = 0; i < N; i++)
        B[i] = totalXOR ^ A[i];
 
    // Print the original array B[]
    for (int i = 0; i < N; i++) {
        cout << B[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int A[] = { 13, 14, 10, 6 }, K = 2;
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    constructArray(A, N, K);
 
    return 0;
}


Java




// Java program for the above approach
class GFG{
     
// Function to construct an array
// with each element equal to XOR
// of all array elements except
// the element at the same index
static void constructArray(int A[], int N,
                           int K)
{
     
    // Original array
    int B[] = new int[N];
   
    // Stores Bitwise XOR of array
    int totalXOR = A[0] ^ K;
   
    // Calculate XOR of all array elements
    for(int i = 0; i < N; i++)
        B[i] = totalXOR ^ A[i];
   
    // Print the original array B[]
    for(int i = 0; i < N; i++)
    {
        System.out.print(B[i] + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 13, 14, 10, 6 }, K = 2;
    int N = A.length;
     
    // Function Call
    constructArray(A, N, K);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3




# Python program for the above approach
 
# Function to construct an array
# with each element equal to XOR
# of all array elements except
# the element at the same index
def constructArray(A, N, K):
   
    # Original array
    B = [0] * N;
 
    # Stores Bitwise XOR of array
    totalXOR = A[0] ^ K;
 
    # Calculate XOR of all array elements
    for i in range(N):
        B[i] = totalXOR ^ A[i];
 
    # Print the original array B
    for i in range(N):
        print(B[i], end = " ");
 
# Driver Code
if __name__ == '__main__':
    A = [13, 14, 10, 6];
    K = 2;
    N = len(A);
 
    # Function Call
    constructArray(A, N, K);
 
# This code is contributed by Princi Singh


C#




// C# program for the above approach
using System;
using System.Collections;
class GFG {
     
    // Function to construct an array
    // with each element equal to XOR
    // of all array elements except
    // the element at the same index
    static void constructArray(int[] A, int N,
                               int K)
    {
          
        // Original array
        int[] B = new int[N];
        
        // Stores Bitwise XOR of array
        int totalXOR = A[0] ^ K;
        
        // Calculate XOR of all array elements
        for(int i = 0; i < N; i++)
            B[i] = totalXOR ^ A[i];
        
        // Print the original array B[]
        for(int i = 0; i < N; i++)
        {
            Console.Write(B[i] + " ");
        }
    }
 
  static void Main() {
    int[] A = { 13, 14, 10, 6 };
    int K = 2;
    int N = A.Length;
      
    // Function Call
    constructArray(A, N, K);
  }
}
 
// This code is contributed by divyesh072019


Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to construct an array
// with each element equal to XOR
// of all array elements except
// the element at the same index
function constructArray(A, N, K)
{
    // Original array
    let B = new Array(N);
 
    // Stores Bitwise XOR of array
    let totalXOR = A[0] ^ K;
 
    // Calculate XOR of all array elements
    for (let i = 0; i < N; i++)
        B[i] = totalXOR ^ A[i];
 
    // Print the original array B[]
    for (let i = 0; i < N; i++) {
        document.write(B[i] + " ");
    }
}
 
// Driver Code
    let A = [ 13, 14, 10, 6 ], K = 2;
    let N = A.length;
 
    // Function Call
    constructArray(A, N, K);
 
// This code is contributed by Surbhi Tyagi.
 
</script>


Output: 

2 1 5 9

 

Time Complexity: O(N)
Auxiliary Space: O(1)



Similar Reads

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} Recommended PracticeXOR of all elementsTry It! Naive Approach : We can simple cal
5 min read
Bitwise XOR of same indexed array elements after rearranging an array to make XOR of same indexed elements of two arrays equal
Given two arrays A[] and B[] consisting of N positive integers, the task is to the Bitwise XOR of same indexed array elements after rearranging the array B[] such that the Bitwise XOR of the same indexed elements of the arrays A[] becomes equal. Examples: Input: A[] = {1, 2, 3}, B[] = {4, 6, 7}Output: 5Explanation:Below are the possible arrangement
14 min read
Construct an array whose Prefix XOR array starting from X is an N-length increasing sequence
Given two integers N and X, the task is to generate an array of size N, such that the prefix xor array of X with the generated array will be permutations of 1st N natural numbers. Examples: Input: N = 4, X = 3Output: [2, 3, 1, 7]Explanation: Prefix XOR array for the array {2, 3, 1, 7} is as follows: X ? 2 = 1. Now, X = 1X ? 3 = 2. Now, X = 2X ? 1 =
4 min read
Queries to calculate sum of array elements present at every Yth index starting from the index X
Given an array arr[] of size N, and an array Q[][] with each row representing a query of the form { X, Y }, the task for each query is to find the sum of array elements present at indices X, X + Y, X + 2 * Y + ... Examples: Input: arr[] = { 1, 2, 7, 5, 4 }, Q[][] = { { 2, 1 }, { 3, 2 } } Output: 16 5 Explanation: Query1: arr[2] + arr[2 + 1] + arr[2
21 min read
Find the original Array using XOR values of all adjacent elements
Given a sequence arr[] of N-1 elements which is xor of all adjacent pairs in an array, the task is to find that original array from the arr[].Note: It is given that the N is always odd and arr[] contains the permutation of N natural number.Examples: Input: arr[] = {3, 1} Output: 1 2 3 Explanation: The XOR of the output array will lead to the given
9 min read
Find smallest Subarray with Maximum Xor starting from each index
Given an array A[] consisting of N elements, the task is to find the minimum length of the subarray starting from each index and the bitwise OR value is the maximum among all possible subarrays starting from that index. Examples: Input: A[] = [4, 5, 2, 9, 11]Output: [4, 3, 2, 2, 1]Explanation: For i=0, subarray [4, 5, 2, 9] is having maximum OR of
10 min read
Minimum index i such that all the elements from index i to given index are equal
Given an array arr[] of integers and an integer pos, the task is to find the minimum index i such that all the elements from index i to index pos are equal. Examples: Input: arr[] = {2, 1, 1, 1, 5, 2}, pos = 3 Output: 1 Elements in index range [1, 3] are all equal to 1. Input: arr[] = {2, 1, 1, 1, 5, 2}, pos = 5 Output: 5 Brute Force Approach: A br
12 min read
Reverse all elements of given circular array starting from index K
Given a circular array arr[] of size N and an index K, the task is to reverse all elements of the circular array starting from the index K. Examples: Input: arr[] = {3, 5, 2, 4, 1}, K = 2Output: 4 2 5 3 1Explanation:After reversing the elements of the array from index K to K - 1, the modified arr[] is {4, 1, 2, 5, 3}. Input: arr[] = {1, 2, 3, 4, 5}
7 min read
Construct longest Array starting with N and A[i] as multiple of A[i+1]
Given an integer N, the task is to construct the longest possible array A[], such that the following conditions hold: A[0] = N.No two adjacent elements should be equal.For all i (0 &lt; i &lt; array length), such that A[i] is divisible by A[i + 1] Note: If there are many possible sequence, print any sequence. Examples: Input: N = 10Output: 3, {10,
8 min read
Count subarrays having total distinct elements same as original array
Given an array of n integers. Count the total number of sub-arrays having total distinct elements, the same as that of the total distinct elements of the original array. Examples: Input : arr[] = {2, 1, 3, 2, 3} Output : 5 Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from
11 min read
Article Tags :
Practice Tags :