Profile photo for Quora User

We can get this maximum subset xor of some elements using an idea similar to Gaussian elimination.

From Stack exchange:

Let the “length” of a number be the number of digits needed to write it out in binary, excluding any leading zeros. Equivalently, the length of a number is the position of the leading 11 bit in its binary representation.

Clearly, if all the input numbers had a different length, the problem would have a trivial solution: just iterate over the input numbers in decreasing order by length, choosing each number if and only if XORing it with the maximum so far increases the maximum, i.e. if and only if its leading bit is not set in the current maximum.

The tricky part is when the input may contain multiple numbers with the same length since then it’s not obvious which of them we should choose to include in the XOR. What we’d like to do is reduce the input list into an equivalent form that doesn’t contain more than one number of the same length.

Conveniently, this is exactly what Gaussian elimination does: it transforms a list of vectors into another list of vectors which have strictly decreasing length, as defined above but which still spans the same linear subspace.

The reason this linear algebra algorithm is relevant here is that binary numbers satisfy the axioms of a vector space over the finite field of two elements, a.k.a. GF(2), with the numbers viewed as vectors of bits, and with XOR as the vector addition operation.

You can read more at:

Maximization with xor operator

Find the maximum subset XOR of a given set - GeeksforGeeks

Gaussian Elimination for XOR Maximization - Bhavesh Munot

SPOJ – XMAX

XOR with Subset

View 2 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025