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 arrayvoid add(vector<int>& arr, int x){ arr.push_back(x);}// Function to update every array// element a[i] by a[i] ^ xvoid update(int& effect, int x){ effect = effect ^ x;}// Function to compute the final// results after the operationsvoid computeResults(vector<int>& arr, int effect){ for (int i = 0; i < arr.size(); i++) { arr[i] = arr[i] ^ effect; cout << arr[i] << " "; }}// Driver Codesigned 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 approachimport java.util.*;class GFG{ static Vector<Integer> arr = new Vector<Integer>();static int effect; // Function to insert an element// into the arraystatic void add(int x){ arr.add(x);}// Function to update every array// element a[i] by a[i] ^ xstatic void update(int x){ effect = effect ^ x;}// Function to compute the final// results after the operationsstatic void computeResults(){ for (int i = 0; i < arr.size(); i++) { arr.set(i, arr.get(i) ^ effect); System.out.print(arr.get(i) + " "); }}// Driver Codepublic 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 arraydef add(arr, x): arr.append(x)# Function to update every array# element a[i] by a[i] ^ xdef update(effect, x): effect = effect ^ x return effect# Function to compute the final# results after the operationsdef computeResults(arr, effect): for i in range(len(arr)): arr[i] = arr[i] ^ effect print(arr[i], end = " ")# Driver Codeif __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 approachusing System;using System.Collections.Generic;class GFG{ static List<int> arr = new List<int>();static int effect; // Function to insert an element// into the arraystatic void add(int x){ arr.Add(x);}// Function to update every array// element a[i] by a[i] ^ xstatic void update(int x){ effect = effect ^ x;}// Function to compute the final// results after the operationsstatic void computeResults(){ for (int i = 0; i < arr.Count; i++) { arr[i] = arr[i] ^ effect; Console.Write(arr[i] + " "); }}// Driver Codepublic 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 arrayfunction add(arr, x){ arr.push(x);}// Function to update every array// element a[i] by a[i] ^ xfunction update(effect, x){ effect = effect ^ x; return effect;}// Function to compute the final// results after the operationsfunction computeResults(arr, effect) { for (let i = 0; i < arr.length; i++) { arr[i] = arr[i] ^ effect; document.write(arr[i] + " "); }}// Driver Codelet arr = [0];let effect = 0;// Queriesadd(arr, 5);effect = update(effect, 2)// Function CallcomputeResults(arr, effect);// This code is contributed by gfgking.</script> |
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


