Maximum Subset XOR
Problem Statement
You get an array arr of positive integers. Pick any group of its elements. The group can be one element, all of them, or anything in between. XOR the chosen elements together.
Return the largest value that any such group can give. Every element is at least 1, so one element on its own already gives the largest element. The answer can be bigger than that.
An array arr of length n.
One integer: the largest XOR of any group of elements from arr. The function is int maxSubsetXOR(vector<int> &arr).
2 XOR 5 = 010 XOR 101 = 111 = 7, the largest value. (2 XOR 4 XOR 5 = 3, and 4 XOR 5 = 1.)
8 XOR 5 = 1000 XOR 0101 = 1101 = 13. The pair 9 XOR 5 = 12 is smaller.
- • 1 ≤ n ≤ 10^5
- • 1 ≤ arr[i] ≤ 10^6
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try all 2^N subsets (each element in or out), XOR each one, keep the largest
Keep every XOR value reachable so far; each new number adds v ^ x for each value v
Keep one number per highest bit; insert by XOR-ing away high bits; build the answer from the top bit down
Each subset is a bitmask: bit i set means arr[i] is in the group. Loop over all 2^N masks, XOR the chosen elements of each one, and keep the largest result. This tries every choice, so it is correct, but the work doubles with every new element.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.