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 |
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.
Recommended Posts:
- Replace every array element by Bitwise Xor of previous and next element
- Replace every element of the array by its previous element
- Replace every element of the array by its next element
- Replace every element with the least greater element on its right
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Replace every element of the array with BitWise XOR of all other
- Replace every element of array with sum of elements on its right side
- Replace every elements in the array by its frequency in the array
- Queries to count array elements greater than or equal to a given number with updates
- Queries for count of array elements with values in given range with updates
- Queries to find minimum swaps required to sort given array with updates
- Queries to update each element in subarray to Bitwise XOR with a given value
- Array range queries to count the number of Fibonacci numbers with updates
- Array Range Queries to count Powerful numbers with updates
- Array range queries to find the maximum Fibonacci number with updates
- Array Range Queries to find the Maximum Armstrong number with updates
- Range Queries for number of Armstrong numbers in an array with updates
- Queries to find the Lower Bound of K from Prefix Sum Array with updates using Fenwick Tree
- Array range queries to find the number of perfect square elements with updates
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

