Skip to content

Commit 597efa3

Browse files
committed
1829
1 parent fed5db2 commit 597efa3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# @param {Integer[]} nums
2+
# @param {Integer} maximum_bit
3+
# @return {Integer[]}
4+
def get_maximum_xor(nums, maximum_bit)
5+
# since nums[i] < 2**maximum_bit and k < 2**maximum_bit
6+
# so the XOR result always < 2**maximum_bit
7+
# that mean the max is 2**maximum_bit - 1
8+
max = 2**maximum_bit - 1
9+
prefix_xor = 0
10+
nums.map { |num|
11+
prefix_xor ^= num
12+
# to get the max, we need eliminate the prefix_xor
13+
# then xor with the max
14+
# prefix_xor ^ k = max
15+
# => k = prefix_xor ^ max
16+
prefix_xor ^ max
17+
}
18+
.reverse
19+
end

0 commit comments

Comments
 (0)