Open In App

Queries to replace every array element by its XOR with a given value with updates

Given an array, initially consisting of 0 as the only element present and Q operations of the following two types:

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:

Below is the implementation of the above approach:




// 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 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 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# 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




<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)


Article Tags :