
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find maximum XOR of given integer in a stream of integers in C++
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
#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
- Related Articles
- Median in a stream of integers (running integers) in C++
- Find a value whose XOR with given number is maximum in C++
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Find Maximum XOR value of a sub-array of size k in C++
- Maximum GCD of N integers with given product in C++
- Maximum number of Unique integers in Sub- Array of given sizes in C++
- Find a pair with maximum product in array of Integers in C++
- Generate Infinite Stream of Integers in Java using Random.ints()
- Generate Infinite Stream of Integers in Java using IntStream.generate()
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Show that the following integers are cubes of negative integers. Also, find the integer whose cube is the given integer:(i) $-5832$(ii) $-2744000$
- Maximum XOR value of a pair from a range in C++
- Find a number which give minimum sum when XOR with every number of array of integer in Python
- Find a number which give minimum sum when XOR with every number of array of integer in C++
- Maximum XOR of Two Numbers in an Array in C++
