We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fed5db2 commit 597efa3Copy full SHA for 597efa3
1801-1900/1829_maximum_xor_for_each_query.rb
@@ -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