 Open in App
Not now

# Find element which maximize the XOR value for N given update query

• Last Updated : 18 Feb, 2023

Given arrays Arr[] and Queries[] of size N and an integer K such that (0 ≤ Arr[i], Queries[i] < 2K), the task is to form an array, where each element represents an integer such that XOR of all elements of array and element is maximum by updating Arr[i] with Queries[i] at each step (0 ≤ i ≤ N-1).

Examples:

Input: N = 4, Arr[] = {2, 3, 4, 7}, Queries[] = {1, 0, 3, 4}, K = 4
Output: {14, 13, 10, 9}
Explanation: The queries are answered as follows:

• 1st query: Arr[] = {2, 3, 4, 7}, After Updating Arr[] = {1, 3, 4, 7} then Choosing X = 14 since 1 XOR 3 XOR 4 XOR 7 XOR 14 = 15 which is the maximum.
• 2nd query: Arr[] = {1, 3, 4, 7}, After Updating Arr[] = {1, 0, 4, 7} then Choosing X = 13 since 1 XOR 0 XOR 4 XOR 7 XOR 13 = 15 which is the maximum.
• 3rd query: Arr[] = {1, 0, 4, 7}, After Updating Arr[[ = {1, 0, 3, 7} then Choosing X = 10 since 1 XOR 0 XOR 3 XOR 7 XOR 10 = 15 which is the maximum.
• 1st query: Arr[] = {1, 0, 3, 7}, After Updating Arr[] = {1, 0, 3, 4 ] then Choosing X = 9 since 1 XOR 0 XOR 3 XOR 4 XOR 9 = 15 which is the maximum.

Input: N = 3, Arr[] = {0, 1, 3},  Queries[] = {2, 2, 2}, K = 3
Output: {7, 4, 5}
Explanation: The queries are answered as follows :

• 1st query: Arr[] = {0, 1, 3}, After Updating Arr[] = {2, 1, 3} then Choosing X = 7 since 2 XOR 1 XOR 3 XOR 7 = 7 which is the maximum .
• 2nd query: Arr[] = {2, 1, 3}, After Updating Arr[] = {2, 2, 3} then Choosing X = 4 since 2 XOR 2 XOR 3 XOR 4 = 7 which is the maximum .
• 3rd query: Arr[] = {2, 2, 3}, After Updating Arr[] = {2, 2, 2} then Choosing X = 5 since 2 XOR 2 XOR 2 XOR 5 = 7 which is the maximum.

Approach:  This problem can be solved with the following idea:

Calculate xor of the array Arr[] and let it be Q, We know the xor property that a xor b = c then a xor c = b or b xor c = a . We need xor to be maximum for every query so rhs will always be (2K – 1) then the equation becomes Q ^ X = (2K – 1) or X = Q ^ (2K – 1) .

Follow the steps to implement the idea:

• Initialize preXor with 0.
• Run a for loop from i : 0 to N and compute the Xor of the array and store it in preXor.
• Initialize rhs with (1 << K) – 1
• Run a loop from i : 0 to N and perform the following steps:
• update preXor = ( preXor ^ Arr[i] )
• update preXor = ( preXor ^ Queries[i] ) .
• Store ( rhs ^ preXor ) in the Answer array.

Below is the implementation of the above algorithm :

## C++

 `// C++ code for the above approach` `#include ` `using` `namespace` `std;`   `// Function performing calculation` `vector<``int``> MaxXor(``int` `N, vector<``int``>& Arr,` `                   ``vector<``int``>& Queries, ``int` `K)` `{`   `    ``vector<``int``> answer;` `    ``int` `preXor = 0;`   `    ``// Precomputing the Xor of the array` `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``preXor = (preXor ^ Arr[i]);` `    ``}`   `    ``// Right hand side variable which have` `    ``// to be maximum` `    ``int` `rhs = (1 << K) - 1;`   `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``// Updating preXor for each update` `        ``// query and the result in` `        ``// answer array` `        ``preXor = (preXor ^ Arr[i]);`   `        ``preXor = (preXor ^ Queries[i]);` `        ``answer.push_back(rhs ^ preXor);` `    ``}`   `    ``// Returning the answer array` `    ``return` `answer;` `}`   `// Function for printing the array` `void` `print(``int` `N, vector<``int``>& answer)` `{`   `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``cout << answer[i] << ``" "``;` `    ``}` `}`   `// Driver code` `int` `main()` `{`   `    ``int` `N = 4;` `    ``vector<``int``> Arr = { 2, 3, 4, 7 };` `    ``vector<``int``> Queries = { 1, 0, 3, 4 };` `    ``int` `K = 4;`   `    ``// Function call` `    ``vector<``int``> answer = MaxXor(N, Arr, Queries, K);`   `    ``// Function call to print` `    ``print(N, answer);`   `    ``return` `0;` `}`

## Java

 `// Java code for the above approach` `import` `java.util.*;`   `public` `class` `Main {` `    ``// Function performing calculation` `    ``public` `static` `List MaxXor(``int` `N, List Arr,` `    ``List Queries, ``int` `K) {` `        ``List answer = ``new` `ArrayList<>(); ``// list to store the result` `        ``int` `preXor = ``0``; ``// variable to store the Xor of the array`   `        ``// Precomputing the Xor of the array` `        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``preXor = (preXor ^ Arr.get(i));` `        ``}` `// Right hand side variable which have to be maximum` `        ``int` `rhs = (``1` `<< K) - ``1``; `   `        ``for` `(``int` `i = ``0``; i < N; i++) {` `      ``// Updating preXor for each update query and the result in answer array` `            ``preXor = (preXor ^ Arr.get(i));` `            ``preXor = (preXor ^ Queries.get(i));` `            ``answer.add(rhs ^ preXor);` `        ``}`   `        ``// Returning the answer list` `        ``return` `answer;` `    ``}`   `    ``// Function for printing the result` `    ``public` `static` `void` `print(``int` `N, List answer) {` `        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``System.out.print(answer.get(i) + ``" "``);` `        ``}` `    ``}`   `    ``public` `static` `void` `main(String[] args) {` `        ``int` `N = ``4``;` `        ``List Arr = Arrays.asList(``2``, ``3``, ``4``, ``7``);` `        ``List Queries = Arrays.asList(``1``, ``0``, ``3``, ``4``);` `        ``int` `K = ``4``;`   `        ``List answer = MaxXor(N, Arr, Queries, K);`   `        ``print(N, answer);` `    ``}` `}`

## Python3

 `#Python code for the above approach` `from` `typing ``import` `List`   `# Function performing calculation` `def` `MaxXor(N: ``int``, Arr: ``List``[``int``], Queries: ``List``[``int``], K: ``int``) ``-``> ``List``[``int``]:` `    ``answer ``=` `[]` `    ``preXor ``=` `0`   `    ``# Precomputing the Xor of the array` `    ``for` `i ``in` `range``(N):` `        ``preXor ``=` `(preXor ^ Arr[i])`   `    ``# Right hand side variable which have` `    ``# to be maximum` `    ``rhs ``=` `(``1` `<< K) ``-` `1`   `    ``for` `i ``in` `range``(N):` `        ``# Updating preXor for each update` `        ``# query and the result in` `        ``# answer array` `        ``preXor ``=` `(preXor ^ Arr[i])` `        ``preXor ``=` `(preXor ^ Queries[i])` `        ``answer.append(rhs ^ preXor)`   `    ``# Returning the answer array` `    ``return` `answer`   `# Function for printing the array` `def` `print_ans(N: ``int``, answer: ``List``[``int``]):` `    ``for` `i ``in` `range``(N):` `        ``print``(answer[i], end ``=` `" "``)` `    ``print``()`   `# Driver code` `if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `4` `    ``Arr ``=` `[``2``, ``3``, ``4``, ``7``]` `    ``Queries ``=` `[``1``, ``0``, ``3``, ``4``]` `    ``K ``=` `4`   `    ``# Function call` `    ``answer ``=` `MaxXor(N, Arr, Queries, K)`   `    ``# Function call to print` `    ``print_ans(N, answer)`   `    ``# This code is contributed by lokehpotta20.`

## C#

 `// C# code for the above approach`   `using` `System;` `using` `System.Linq;` `using` `System.Collections.Generic;`   `class` `GFG ` `{`   `    ``// Function performing calculation` `    ``static` `List<``int``> MaxXor(``int` `N, List<``int``> Arr, List<``int``> Queries, ``int` `K)` `    ``{` `    `  `        ``List<``int``> answer=``new` `List<``int``>();` `        ``int` `preXor = 0;` `    `  `        ``// Precomputing the Xor of the array` `        ``for` `(``int` `i = 0; i < N; i++) {` `    `  `            ``preXor = (preXor ^ Arr[i]);` `        ``}` `    `  `        ``// Right hand side variable which have` `        ``// to be maximum` `        ``int` `rhs = (1 << K) - 1;` `    `  `        ``for` `(``int` `i = 0; i < N; i++) {` `    `  `            ``// Updating preXor for each update` `            ``// query and the result in` `            ``// answer array` `            ``preXor = (preXor ^ Arr[i]);` `    `  `            ``preXor = (preXor ^ Queries[i]);` `            ``answer.Add(rhs ^ preXor);` `        ``}` `    `  `        ``// Returning the answer array` `        ``return` `answer;` `    ``}` `    `  `    ``// Function for printing the array` `    ``static` `void` `print(``int` `N, List<``int``> answer)` `    ``{` `    `  `        ``for` `(``int` `i = 0; i < N; i++) {` `            ``Console.Write(answer[i] + ``" "``);` `        ``}` `    ``}` `    `  `    ``// Driver code` `    ``static` `public` `void` `Main()` `    ``{` `    `  `        ``int` `N = 4;` `        ``List<``int``> Arr = ``new` `List<``int``>{ 2, 3, 4, 7 };` `        ``List<``int``> Queries = ``new` `List<``int``>{ 1, 0, 3, 4 };` `        ``int` `K = 4;` `    `  `        ``// Function call` `        ``List<``int``> answer = MaxXor(N, Arr, Queries, K);` `    `  `        ``// Function call to print` `        ``print(N, answer);` `    `  `    ``}` `}`

## Javascript

 `// Javascript code for the above approach`   `// Function performing calculation` `function` `MaxXor(N, Arr, Queries, K)` `{`   `    ``let answer=[];` `    ``let preXor = 0;`   `    ``// Precomputing the Xor of the array` `    ``for` `(let i = 0; i < N; i++) {`   `        ``preXor = (preXor ^ Arr[i]);` `    ``}`   `    ``// Right hand side variable which have` `    ``// to be maximum` `    ``let rhs = (1 << K) - 1;`   `    ``for` `(let i = 0; i < N; i++) {`   `        ``// Updating preXor for each update` `        ``// query and the result in` `        ``// answer array` `        ``preXor = (preXor ^ Arr[i]);`   `        ``preXor = (preXor ^ Queries[i]);` `        ``answer.push(rhs ^ preXor);` `    ``}`   `    ``// Returning the answer array` `    ``return` `answer;` `}`   `// Function for printing the array` `function` `print(N,  answer)` `{`   `    ``for` `(let i = 0; i < N; i++) {` `        ``console.log(answer[i] + ``" "``);` `    ``}` `}`   `// Driver code` `let N = 4;` `let Arr = [ 2, 3, 4, 7 ];` `let Queries = [ 1, 0, 3, 4 ];` `let K = 4;`   `// Function call` `let answer = MaxXor(N, Arr, Queries, K);`   `// Function call to print` `print(N, answer);`

Output

`14 13 10 9 `

Time Complexity: O(N)
Auxiliary Space: O(N)

Related Articles:

My Personal Notes arrow_drop_up
Related Articles