Given an array arr[] consisting of N distinct integers and queries Q[][] of the type {X, Y}, the task for each query is to find the bitwise XOR of all the array elements after replacing X by Y in the array.
Examples:
Input: arr[] = {1, 2, 3, 4, 5} Q = {(1, 4) (3, 6) (2, 3)}Â
Output:4 1 0Â
Explanation:Â
Query 1: The array modifies to {4, 2, 3, 4, 5} and XOR = 4Â
Query 2: The array modifies to {4, 2, 6, 4, 5} and XOR = 1Â
Query 3: The array modifies to {4, 3, 6, 4, 5} and XOR = 0
Input: arr[] = {5, 7, 8, 9, } Q = {(5, 6) (8, 1)}Â
Output: 0 9Â
Explanation:Â
Query 1: The array modifies to {6, 7, 8, 9} and XOR = 0Â
Query 2: The array modifies to {6, 7, 1, 9} and XOR = 9
Approach:Â
The approach is to use the Bitwise XOR property:
- Suppose, there are three elements A, B, and and X, and their Xor is, total_xor = A ^ B ^ X.
- To remove the contribution of X from total_xor, perform total_xor ^ X. It can be verified from the fact that A ^ B ^ X ^ X = A ^ B (XOR of an element with itself is 0).
- To add the contribution of Y in the total_xor, simply perform total_xor ^ Y.
Below is the implementation of the above approach:
C++
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std;Â
// Stores the bitwise XOR// of array elementsint total_xor;Â
// Function to find the total xorvoid initialize_xor(int arr[], int n){    // Loop to find the xor    // of all the elements    for (int i = 0; i < n; i++) {        total_xor = total_xor ^ arr[i];    }}Â
// Function to find the XOR// after replacing all occurrences// of X by Y for Q queriesvoid find_xor(int X, int Y){    // Remove contribution of    // X from total_xor    total_xor = total_xor ^ X;Â
    // Adding contribution of    // Y to total_xor    total_xor = total_xor ^ Y;Â
    // Print total_xor    cout << total_xor << "\n";}Â
// Driver Codeint main(){Â Â Â Â int arr[] = { 5, 7, 8, 9 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â
    initialize_xor(arr, n);Â
    vector<vector<int> > Q = { { 5, 6 }, { 8, 1 } };Â
    for (int i = 0; i < Q.size(); i++) {        find_xor(Q[i][0], Q[i][1]);    }    return 0;} |
Java
// Java Program to implement// the above approachimport java.util.*;class GFG{Â
// Stores the bitwise XOR// of array elementsstatic int total_xor;Â
// Function to find the total xorstatic void initialize_xor(int arr[],                           int n){    // Loop to find the xor    // of all the elements    for (int i = 0; i < n; i++)     {        total_xor = total_xor ^ arr[i];    }}Â
// Function to find the XOR// after replacing all occurrences// of X by Y for Q queriesstatic void find_xor(int X, int Y){    // Remove contribution of    // X from total_xor    total_xor = total_xor ^ X;Â
    // Adding contribution of    // Y to total_xor    total_xor = total_xor ^ Y;Â
    // Print total_xor    System.out.print(total_xor + "\n");}Â
// Driver Codepublic static void main(String[] args){Â Â Â Â int arr[] = { 5, 7, 8, 9 };Â Â Â Â int n = arr.length;Â
    initialize_xor(arr, n);Â
    int [][]Q = { { 5, 6 }, { 8, 1 } };Â
    for (int i = 0; i < Q.length; i++)    {        find_xor(Q[i][0], Q[i][1]);    }}}Â
// This code is contributed by Rohit_ranjan |
Python3
# Python3 program to implement# the above approachÂ
# Stores the bitwise XOR# of array elementsglobal total_xortotal_xor = 0Â
# Function to find the total xordef initialize_xor(arr, n):Â
    global total_xorÂ
    # Loop to find the xor    # of all the elements    for i in range(n):        total_xor = total_xor ^ arr[i]Â
# Function to find the XOR# after replacing all occurrences# of X by Y for Q queriesdef find_xor(X, Y):Â Â Â Â Â Â Â Â Â global total_xorÂ
    # Remove contribution of    # X from total_xor    total_xor = total_xor ^ XÂ
    # Adding contribution of    # Y to total_xor    total_xor = total_xor ^ YÂ
    # Print total_xor     print(total_xor)Â
# Driver Codeif __name__ == '__main__':Â
    arr = [ 5, 7, 8, 9 ]    n = len(arr)Â
    initialize_xor(arr, n)Â
    Q = [ [ 5, 6 ], [ 8, 1 ] ]Â
    # Function call    for i in range(len(Q)):        find_xor(Q[i][0], Q[i][1])Â
# This code is contributed by Shivam Singh |
C#
// C# program to implement// the above approachusing System;Â
class GFG{Â
// Stores the bitwise XOR// of array elementsstatic int total_xor;Â
// Function to find the total xorstatic void initialize_xor(int []arr,                           int n){         // Loop to find the xor    // of all the elements    for(int i = 0; i < n; i++)     {        total_xor = total_xor ^ arr[i];    }}Â
// Function to find the XOR// after replacing all occurrences// of X by Y for Q queriesstatic void find_xor(int X, int Y){         // Remove contribution of    // X from total_xor    total_xor = total_xor ^ X;Â
    // Adding contribution of    // Y to total_xor    total_xor = total_xor ^ Y;Â
    // Print total_xor    Console.Write(total_xor + "\n");}Â
// Driver Codepublic static void Main(String[] args){Â Â Â Â int []arr = { 5, 7, 8, 9 };Â Â Â Â int n = arr.Length;Â
    initialize_xor(arr, n);Â
    int [,]Q = { { 5, 6 }, { 8, 1 } };Â
    for(int i = 0; i < Q.GetLength(0); i++)    {        find_xor(Q[i, 0], Q[i, 1]);    }}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// JavaScript program for the above approachÂ
// Stores the bitwise XOR// of array elementslet total_xor;   // Function to find the total xorfunction initialize_xor(arr, n){    // Loop to find the xor    // of all the elements    for (let i = 0; i < n; i++)     {        total_xor = total_xor ^ arr[i];    }}   // Function to find the XOR// after replacing all occurrences// of X by Y for Q queriesfunction find_xor(X, Y){    // Remove contribution of    // X from total_xor    total_xor = total_xor ^ X;       // Adding contribution of    // Y to total_xor    total_xor = total_xor ^ Y;       // Print total_xor    document.write(total_xor + "<br/>");}     // Driver Code             let arr = [ 5, 7, 8, 9 ];    let n = arr.length;       initialize_xor(arr, n);       let Q = [[ 5, 6 ], [ 8, 1]];       for (let i = 0; i < Q.length; i++)    {        find_xor(Q[i][0], Q[i][1]);    }                            </script> |
0 9
Â
Time Complexity: O(N + sizeof(Q))Â
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
