Queries to replace every array element by its XOR with a given value with updates
Last Updated :
09 Jun, 2021
Given an array, initially consisting of 0 as the only element present and Q operations of the following two types:
- Add(X): Insert X into the array.
- Update(X): Replace every array element Ai by Ai ^ X, where ^ is the XOR operation.
Examples:
Input: Q = 2
Add(5)
Update(2)
Output: 2 7
Explanation:
Initially, Array = {0}
Query 1: Array = {0, 5}
Query 2: Array = {0^2, 5^2} => {2, 7}
Input: Q = 2
Add(3)
Add(5)
Output: 0 3 5
Approach: The idea is to store a variable that stores the value that is to be XOR‘ed with every array element and at the time of the insertion of the element in the array to remove the effect of the XOR value, apply the XOR operation with itself. Follow the steps below to solve the problem:
- Maintain a variable, say effect, to store the final XOR value.
- For Operation Add(X): Add X at the end of the list and update its value with X ^ effect.
- For Operation Update(X): Update the value of ‘effect’ with effect ^ X. This is for getting an updated XOR effect till now.
- After completing all the above operations, update all array elements with its XOR with effect, i.e. for all Ai, update Ai = Ai ^ effect.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
void add(vector< int >& arr, int x)
{
arr.push_back(x);
}
void update( int & effect, int x)
{
effect = effect ^ x;
}
void computeResults(vector< int >& arr,
int effect)
{
for ( int i = 0; i < arr.size(); i++) {
arr[i] = arr[i] ^ effect;
cout << arr[i] << " " ;
}
}
signed main()
{
vector< int > arr = { 0 };
int effect = 0;
add(arr, 5);
update(effect, 2);
computeResults(arr, effect);
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> arr =
new Vector<Integer>();
static int effect;
static void add( int x)
{
arr.add(x);
}
static void update( int x)
{
effect = effect ^ x;
}
static void computeResults()
{
for ( int i = 0 ; i < arr.size(); i++)
{
arr.set(i, arr.get(i) ^ effect);
System.out.print(arr.get(i) + " " );
}
}
public static void main(String[] args)
{
arr = new Vector<Integer>();
arr.add( 0 );
effect = 0 ;
add( 5 );
update( 2 );
computeResults();
}
}
|
Python3
def add(arr, x):
arr.append(x)
def update(effect, x):
effect = effect ^ x
return effect
def computeResults(arr, effect):
for i in range ( len (arr)):
arr[i] = arr[i] ^ effect
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 0 ]
effect = 0
add(arr, 5 )
effect = update(effect, 2 )
computeResults(arr, effect)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > arr = new List< int >();
static int effect;
static void add( int x)
{
arr.Add(x);
}
static void update( int x)
{
effect = effect ^ x;
}
static void computeResults()
{
for ( int i = 0; i < arr.Count; i++)
{
arr[i] = arr[i] ^ effect;
Console.Write(arr[i] + " " );
}
}
public static void Main(String[] args)
{
arr = new List< int >();
arr.Add(0);
effect = 0;
add(5);
update(2);
computeResults();
}
}
|
Javascript
<script>
function add(arr, x)
{
arr.push(x);
}
function update(effect, x)
{
effect = effect ^ x;
return effect;
}
function computeResults(arr, effect) {
for (let i = 0; i < arr.length; i++) {
arr[i] = arr[i] ^ effect;
document.write(arr[i] + " " );
}
}
let arr = [0];
let effect = 0;
add(arr, 5);
effect = update(effect, 2)
computeResults(arr, effect);
</script>
|
Time Complexity: O(Q)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...