Queries to find the XOR of an Array after replacing all occurrences of X by Y
Last Updated :
11 May, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int total_xor;
void initialize_xor( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
total_xor = total_xor ^ arr[i];
}
}
void find_xor( int X, int Y)
{
total_xor = total_xor ^ X;
total_xor = total_xor ^ Y;
cout << total_xor << "\n" ;
}
int 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
import java.util.*;
class GFG{
static int total_xor;
static void initialize_xor( int arr[],
int n)
{
for ( int i = 0 ; i < n; i++)
{
total_xor = total_xor ^ arr[i];
}
}
static void find_xor( int X, int Y)
{
total_xor = total_xor ^ X;
total_xor = total_xor ^ Y;
System.out.print(total_xor + "\n" );
}
public 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 ]);
}
}
}
|
Python3
global total_xor
total_xor = 0
def initialize_xor(arr, n):
global total_xor
for i in range (n):
total_xor = total_xor ^ arr[i]
def find_xor(X, Y):
global total_xor
total_xor = total_xor ^ X
total_xor = total_xor ^ Y
print (total_xor)
if __name__ = = '__main__' :
arr = [ 5 , 7 , 8 , 9 ]
n = len (arr)
initialize_xor(arr, n)
Q = [ [ 5 , 6 ], [ 8 , 1 ] ]
for i in range ( len (Q)):
find_xor(Q[i][ 0 ], Q[i][ 1 ])
|
C#
using System;
class GFG{
static int total_xor;
static void initialize_xor( int []arr,
int n)
{
for ( int i = 0; i < n; i++)
{
total_xor = total_xor ^ arr[i];
}
}
static void find_xor( int X, int Y)
{
total_xor = total_xor ^ X;
total_xor = total_xor ^ Y;
Console.Write(total_xor + "\n" );
}
public 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]);
}
}
}
|
Javascript
<script>
let total_xor;
function initialize_xor(arr, n)
{
for (let i = 0; i < n; i++)
{
total_xor = total_xor ^ arr[i];
}
}
function find_xor(X, Y)
{
total_xor = total_xor ^ X;
total_xor = total_xor ^ Y;
document.write(total_xor + "<br/>" );
}
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>
|
Time Complexity: O(N + sizeof(Q))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...