that's a relief to see so many people confused, I'm neither a native English speaker nor a computer genius, I heavily questioned my entire life while reading this problem
Let's say the current xor is 101010 and the mask is 001111. Wouldn't appending (xor ^ mask) to the results array give [100101] instead of [0101]. I feel like the more appropriate statement should be res.append((xor ^ mask) & mask) Since it only appends the last 4 bits. Or maybe i'm just trippin idk.
if we apply mask , it actually works differently by taking only required bits, in your case its last 4 bits and omitting the other bits in the result, it doesn't works using traditional xor rules here if we use mask, it takes last 4 bits only and appends to answer array
Good point, I forgot to mention the constraint others comments above me mentioned. The example you're referring to wouldn't actually exist, I should've used a different example.
Simpler solution It's just easier to go in the forward and keep track of the xor result. Then xor that with kMax and append to answer. You return the reverse of the answer list def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]: kMax = 2**maximumBit - 1 ans = [nums[0] ^ kMax] currXor = nums[0] for i in range(1, len(nums)): currXor = currXor ^ nums[i] ans.append(currXor ^ kMax) return ans[::-1]
Your explanation is extremely elegant, but this solution only works if all the elements in nums are < 2**maxBit, which is the case in this problem. Which wasn't clear from your video, why that is the case
The compact version and the true O(1) space solution of NeetCode's. def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]: max_num = (1
I really came here to underastand the problem description.
It feels like the author of the problem first came up with the solution and then drafted a problem for the solution.
that's a relief to see so many people confused, I'm neither a native English speaker nor a computer genius, I heavily questioned my entire life while reading this problem
Let's say the current xor is 101010 and the mask is 001111. Wouldn't appending (xor ^ mask) to the results array give [100101] instead of [0101].
I feel like the more appropriate statement should be res.append((xor ^ mask) & mask) Since it only appends the last 4 bits.
Or maybe i'm just trippin idk.
The problem constrains say that the mask is always greater than the lasted possible value in nums
if we apply mask , it actually works differently by taking only required bits, in your case its last 4 bits and omitting the other bits in the result, it doesn't works using traditional xor rules here if we use mask, it takes last 4 bits only and appends to answer array
U are correct, but here in this problem constraints it's mentioned that nums[i] < 2**maxBit. So it works out fine
Good point, I forgot to mention the constraint others comments above me mentioned.
The example you're referring to wouldn't actually exist, I should've used a different example.
I had a solution for the general case but it included strings and vudu and I had the same question as you did =)))
i came here to understand the question but im still confused its over for me
Simpler solution
It's just easier to go in the forward and keep track of the xor result. Then xor that with kMax and append to answer. You return the reverse of the answer list
def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:
kMax = 2**maximumBit - 1
ans = [nums[0] ^ kMax]
currXor = nums[0]
for i in range(1, len(nums)):
currXor = currXor ^ nums[i]
ans.append(currXor ^ kMax)
return ans[::-1]
I first came here to understand the problem description, and then I came here to understand the solution😁
Bro u hacking them or wut
thank you. what a confusing problem description
Now I understand what the problem is... i mean Wow 😵💫
You are a legend
thank you for the explanation!
Brut force solution got time limited exceeded :D
Your explanation is extremely elegant, but this solution only works if all the elements in nums are < 2**maxBit, which is the case in this problem. Which wasn't clear from your video, why that is the case
I am kneeling 🙏🏻
Neetcode!!!!!!!
thanks :)
is we have a same daily problem in leetcode?
yessir
Bits problems sucks ! 👺
The compact version and the true O(1) space solution of NeetCode's.
def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:
max_num = (1