Skip to content
Related Articles

Related Articles

Improve Article
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++




// C++ program of the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
 
// Function to insert an element
// into the array
void add(vector<int>& arr, int x)
{
    arr.push_back(x);
}
 
// Function to update every array
// element a[i] by a[i] ^ x
void update(int& effect, int x)
{
    effect = effect ^ x;
}
 
// Function to compute the final
// results after the operations
void computeResults(vector<int>& arr,
                    int effect)
{
    for (int i = 0; i < arr.size(); i++) {
        arr[i] = arr[i] ^ effect;
        cout << arr[i] << " ";
    }
}
 
// Driver Code
signed main()
{
    vector<int> arr = { 0 };
    int effect = 0;
 
    // Queries
    add(arr, 5);
    update(effect, 2);
 
    // Function Call
    computeResults(arr, effect);
}

Java




// Java program of
// the above approach
import java.util.*;
class GFG{
   
static Vector<Integer> arr =
              new Vector<Integer>();
static int effect;
   
// Function to insert an element
// into the array
static void add(int x)
{
  arr.add(x);
}
 
// Function to update every array
// element a[i] by a[i] ^ x
static void update(int x)
{
  effect = effect ^ x;
}
 
// Function to compute the final
// results after the operations
static void computeResults()
{
  for (int i = 0; i < arr.size(); i++)
  {
    arr.set(i, arr.get(i) ^ effect);
    System.out.print(arr.get(i) + " ");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  arr = new Vector<Integer>();
  arr.add(0);
  effect = 0;
 
  // Queries
  add(5);
  update(2);
 
  // Function Call
  computeResults();
}
}
 
// This code is contributed by Rajput-Ji

Python3




# Python3 program of the above approach
 
# Function to insert an element
# into the array
def add(arr, x):
     
    arr.append(x)
 
# Function to update every array
# element a[i] by a[i] ^ x
def update(effect, x):
 
    effect = effect ^ x
    return effect
 
# Function to compute the final
# results after the operations
def computeResults(arr, effect):
 
    for i in range(len(arr)):
        arr[i] = arr[i] ^ effect
        print(arr[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
 
    arr = [0]
    effect = 0
 
    # Queries
    add(arr, 5)
    effect = update(effect, 2)
 
    # Function call
    computeResults(arr, effect)
 
# This code is contributed by mohit kumar 29

C#




// C# program of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
   
static List<int> arr = new List<int>();
static int effect;
   
// Function to insert an element
// into the array
static void add(int x)
{
  arr.Add(x);
}
 
// Function to update every array
// element a[i] by a[i] ^ x
static void update(int x)
{
  effect = effect ^ x;
}
 
// Function to compute the final
// results after the operations
static void computeResults()
{
  for (int i = 0; i < arr.Count; i++)
  {
    arr[i] = arr[i] ^ effect;
    Console.Write(arr[i] + " ");
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  arr = new List<int>();
  arr.Add(0);
  effect = 0;
 
  // Queries
  add(5);
  update(2);
 
  // Function Call
  computeResults();
}
}
 
// This code is contributed by shikhasingrajput

Javascript




<script>
// Javascript program of the above approach
 
// Function to insert an element
// into the array
function add(arr, x)
{
    arr.push(x);
}
 
// Function to update every array
// element a[i] by a[i] ^ x
function update(effect, x)
{
    effect = effect ^ x;
    return effect;
}
 
// Function to compute the final
// results after the operations
function computeResults(arr, effect) {
    for (let i = 0; i < arr.length; i++) {
        arr[i] = arr[i] ^ effect;
        document.write(arr[i] + " ");
    }
}
 
// Driver Code
let arr = [0];
let effect = 0;
 
// Queries
add(arr, 5);
effect = update(effect, 2)
 
// Function Call
computeResults(arr, effect);
 
// This code is contributed by gfgking.
</script>
Output: 
2 7

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :