Find maximum XOR of given integer in a stream of integers in C++

C++Server Side ProgrammingProgramming

In this problem, we are given Q queries each of which is one of the following type,

Type 1 − insertion (1, i) to add the element with value i, in your data structure.

Type 2 − findXOR (2, i), to find the XOR of all elements of the data structure with the element i.

The data structure should contain only 1 element initially which will be 0.

Let’s take an example to understand the problem,

Input

Queries: (1, 9), (1, 3), (1, 7), (2, 8), (1, 5), (2, 12)

Output

15 15

Explanation

Solving each query,
(1, 9) => data structure => {9}
(1, 3) => data structure => {9, 3}
(1, 7) => data structure => {9, 3, 7}
(2, 8) => maximum XOR(_, 8) = 15, XOR(7, 8)
(1, 5) => data structure => {9, 3, 7, 5}
(2, 12) => maximum XOR(_, 12) = 15, XOR(3, 12)

Solution Approach

The solution to the problem can be found using the trie data structure, which is a special type of search tree. We will use a trie in which each node has two child nodes to store the binary values of a number. After this we will add the number’s binary value to the trie for each query of type 1. For a query of type 2, we will find the path in trie for the given value and then the level count will give the result.

For more on trie visit, trie data structure.

Program to illustrate the working of our solution,

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
struct Trie {
   Trie* children[2];
   bool isLeaf;
};
bool check(int N, int i) {
   return (bool)(N & (1<<i));
}
Trie* newNode() {
   Trie* temp = new Trie;
   temp->isLeaf = false;
   temp->children[0] = NULL;
   temp->children[1] = NULL;
   return temp;
}
void insertVal(Trie* root, int x) {
   Trie* val = root;
   for (int i = 31; i >= 0; i--) {
      int f = check(x, i);
      if (! val->children[f])
         val->children[f] = newNode();
         val = val->children[f];
   }
   val->isLeaf = true;
}
int solveQueryType2(Trie *root, int x){
   Trie* val = root;
   int ans = 0;
   for (int i = 31; i >= 0; i--) {
      int f = check(x, i);
      if ((val->children[f ^ 1])){
         ans = ans + (1 << i);
         val = val->children[f ^ 1];
      }
      else
      val = val->children[f];
   }
   return ans;
}
void solveQueryType1(Trie *root, int x){
   insertVal(root, x);
}
int main(){
   int Q = 6;
   int query[Q][2] = {{1, 9}, {1, 3}, {1, 7}, {2, 8}, {1, 5}, {2, 12}};
   Trie* root = newNode();
   for(int i = 0; i < Q; i++){
      if(query[i][0] == 1 ){
         solveQueryType1(root, query[i][1]);
         cout<<"Value inserted to the data Structure. value =
         "<<query[i][1]<<endl;
      }
      if(query[i][0] == 2){
         cout<<"The maximum XOR with "<<query[i][1]<<" is
         "<<solveQueryType2(root, query[i][1])<<endl;
      }
   }
   return 0;
}

Output

Value inserted to the data Structure. value = 9
Value inserted to the data Structure. value = 3
Value inserted to the data Structure. value = 7
The maximum XOR with 8 is 15
Value inserted to the data Structure. value = 5
The maximum XOR with 12 is 15
raja
Published on 12-Mar-2021 06:35:54
Advertisements